ef code first - One-to-many relation in ASP.NET MVC4 and CodeFirst -


i'm having problems relationship on asp.net mvc4 , codefirst, returning values these tables related foreign key.

first of all, let's see if i'm doing correctly.

here's example of code:

person class

public class person {     public int id { get; set; }     public string name { get; set; }     public string surname { get; set; }     public city city { get; set; } } 

city class

public class city {     public int id { get; set; }     public string name { get; set; } } 

so, this, database created nice looking relationship, , seens work well. have after code, tables this:

person
--id (pk)
--name
--surname
--city_id (fk)

city
--id (pk)
--name

i've populated seed, here's example:

context.person.addorupdate(p => p.name,     new person { name = "me", city = new city { name = "ludlow" } } ); 

and when need retrieve information view, this...

mydatabase.cs

public class leilaodb : dbcontext {     public dbset<person> persons { get; set; }     public dbset<city> cities { get; set; } } 

homecontroller.cs

mydatabase _db = new mydatabase();          public actionresult index()         {             var model = _db.persons.tolist();              return view(model);         } 

home/index.cshtml

@model ienumerable<testingproject.models.person>  @{     viewbag.title = "home page"; }  @foreach (var item in model) {     @html.partial( "_person", item ); } 

_person.cshtml

@model testingproject.models.person  <div>     <h3>@model.name</h3>     @model.city.name </div> 

i receive null exception...

object reference not set instance of object.  description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.   exception details: system.nullreferenceexception: object reference not set instance of object. 

so, what's wrong?

solution:

i found solution,

mydatabase _db = new mydatabase();  public actionresult index() {     var model = _db.persons.tolist();      return view(model); } 

this code retrieves persons, maybe not overload when it's not necessary relationships. need specify when need these relationships include() method.

after this, simple:

mydatabase _db = new mydatabase();  public actionresult index() {     var model = _db.persons.include("city");      return view(model); } 

i feel strange passing string method, it's ok. can return values @model.city.name if need.

i found solution in website here

the entitity framework not relationships unnecessary. if want include others table need call method or make properties virtual lazy relationship.

public virtual city city { get; set; }

this way, lazy mode made.

var model = _db.persons.include("city").tolist();

and way method manualy include relationship tables when necessary.

if want call person , not doing join call _db.persons.tolist();


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -