开发者

entity framework - getting null exception using foreign key

开发者 https://www.devze.com 2022-12-25 01:15 出处:网络
Having some trouble with what should be a very simple scenario. For example purposes, I have two tables:

Having some trouble with what should be a very simple scenario. For example purposes, I have two tables:

-Users -Comments

There is a one-to-many relationship set up for this; there is a foreign key from Comments.CommentorID to Users.UserID. When I do the LINQ query and try to bind to a DataList, I get a null exception. Here is the code:

FKMModel.FKMEntities ctx = new FKMModel.FKMEntities();
        IQueryable<Comment> CommentQuery =
            from x in ctx.Comment
            where x.SiteID == 101 
            select x;
        List<Comment> Comments = CommentQuery.ToList();
        dl_MajorComments.DataSource = Comments;
        dl_MajorComments.DataBind();

In the ASPX page, I have the following as an ItemTemplate (I simplified it and took out the styling, etc, for purposes of posting here since it's irrelevant):

<div>
   <%# ((FKMModel.Comment)Container.DataItem).FKMUser.Username %>
   <%# ((FKMModel.Comment)Container.DataItem).CommentDate.Value.ToShortDateString() %>
   <%# ((FKMModel.Comment)Container.DataItem).CommentTime %>
</div>

The exception occurs on the first binding (FKMUser.Username). Since the foreign key is set up, I should have no problem accessing any properties from the Users table. Intellisense set up the FKMUser navigation property and it knows the properties of that foreign table. Wh开发者_如何学编程at is going on here???


You might need to add the "Include" statement in your query, or disable lazy loading - there's some examples in the following question/.answers: Entity Framework - Inheritance with .Include?

In your example, you'd want to add the Include as follows:

IQueryable<Comment> CommentQuery =
            from x in ctx.Comment
            .Include("<name of FK navigation property">)
            where x.SiteID == 101 
            select x;

Or you can explicitly disable lazy loading for the context - depends on your design as it can be an expensive exercise to do complete object graph loads. I wrote a blog post a while back which addresses some of the "fun" of FKs in EF v1.

Also, you can explicitly ensure the FK (Navigation Property) is loaded by specifically loading it, say on Data Bind.

if (!<entity>.<navigation property>.IsLoaded)   //Ensure the relationship has loaded 
        <entity>.<navigation property>.Load();


This is not because of your coding problem . The entity Key reference mit be zero . i.e in database the foreign key value mit be null . Check the null condition before do something.

FKMModel.FKMEntities ctx = new FKMModel.FKMEntities();
if(ctx.EnitityKey !=null)
{
        IQueryable<Comment> CommentQuery =
            from x in ctx.Comment
            where x.SiteID == 101 
            select x;
        List<Comment> Comments = CommentQuery.ToList();
        dl_MajorComments.DataSource = Comments;
        dl_MajorComments.DataBind();
}

This is a bug in entity framework in .net 3.5 .

or use include Keyword

IQueryable<Comment> CommentQuery =
                from x in ctx.Comment.Include("Users")
                where x.SiteID == 101 
                select x;
0

精彩评论

暂无评论...
验证码 换一张
取 消