entity framework - Why navigation property is always returning null even if the data exists? -


i'm badly stuck on one, i've 1 many relationship between 2 models (pos_cities,pos_company)

pos_cities.cs

public class pos_cities {             [key]     public int id { get; set; }     public string name { get; set; }     public int countryid { get; set; }     public virtual pos_country country { get; set; }                     public virtual icollection<pos_company> company { get; set; } } 

pos_company.cs

public class pos_company {             [key]     public int id { get; set; }     public string name { get; set; }     public string businessname { get; set; }        public int cityid { get; set; }     public virtual pos_cities cities { get; set; } } 

so, scaffold above using entity framework, did not generate code expected, so, had modify code according need, such below index action :

public actionresult index() {     var pos_company = db.pos_company.include(p => p.cities);          return view(pos_company.tolist()); } 

in above code ef did not generate include(p => p.cities) function, so, had add explicitly. now, when execute above index() action, navigation property cities returns null if data present in database :

enter image description here

now, let's make sure data present in database :

data in pos_cities

enter image description here

data in pos_company

enter image description here

so, possibly thing i'm doing wrong? why navigation property cities returning null if data present in database? in advance :)

update

"select [extent1].[id] [id], [extent1].[name] [name], [extent1].[businessname] [businessname], [extent1].[cityid] [cityid], [extent2].[id] [id1], [extent2].[name] [name1], [extent2].[countryid] [countryid]  [dbo].[pos_company] [extent1] left outer join [dbo].[pos_cities] [extent2] on [extent1].[cityid1] = [extent2].[id]" 

try this:

public class pos_company {             [key]     public int id { get; set; }     public string name { get; set; }     public string businessname { get; set; }         [foreignkey("cityid")]     public virtual pos_cities cities { get; set; }     public int cityid { get; set; }  } 

Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -