linq to entities - Why the error 'The cast to value type 'System.Int32' failed because the materialized value is null' when using Include -


i have following poco entity in code first entity framework 6.1.3 application:

public class jobtitle   {     [key, databasegenerated(databasegeneratedoption.identity)]     public virtual int jobtitleid { get; set; }     public virtual string description { get; set; }     public virtual string shortdescription { get; set; }     public virtual int? postid { get; set; }     [foreignkey("postid")]     public virtual post post { get; set; }   } 

the entity configuration follows:

  public class jobtitleconfiguration : entitytypeconfiguration<jobtitle>   {     public jobtitleconfiguration()     {       property(j => j.postid).hascolumnname("post_postid");       hasoptional(j => j.post);     }   } 

the following query gives null value error, if there jobtitles without associated post:

var list=jobtitles.include(j=>j.post).tolist(); 

i not understand why, because foreign key nullable , specified optional in configuration.

how can eager load post information, still avoid error?

a few things check:

  • is postid property inside post class nullable?
  • are there null values "jobtitleid" on database?

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -