entity framework 4.1 invalid column name

笑着哭i 提交于 2019-11-28 08:34:30

The problem is in the relationship between News and NewsCommentView: One end of the relationship is the News.CommentViews collection. But the other end is not NewsCommentView.News as you perhaps expect. Why? Because the property News is not declared on the NewsCommentView class but on the base class NewsComment. Now EF doesn't allow that an entity participates in a relationship with a navigation property which is not declared on that entity class itself but only in a base class.

So, because you don't have Fluent mapping EF defines all relationships only by conventions. What happens?

  • News has a navigation property CommentViews declared and pointing to the NewsCommentView class.
  • EF does not find an inverse property of type News which is declared in the NewsCommentView class. (There is one but it's in the base class, which doesn't count.)
  • So, EF assumes the other end of the relationship is not exposed in the NewsCommentView class.
  • Not exposed means: EF doesn't have a navigation property nor a foreign key property and will assume that the necessary foreign key columns in the database table/view NewsCommentViews will have a standard conventional name.
  • This conventional name is NameOfEntityClass_PKPropertyName -> News_Id

Your real name in the view is NewsId though. So, EF queries for a column News_Id which doesn't exist, hence the exception.

The exception is probably triggered due to lazy loading when your MVC-View accesses NewsComment.News.CommentViews.

You can fix this problem by specifying the FK column name explicitely in Fluent API (as far as I know there no other way without Fluent mapping):

public class MyContext : DbContext
{
    // ...

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<News>()
            .HasMany(n => n.CommentViews)
            .WithRequired() // <- no param because not exposed end of relation,
                            // nc => nc.News would throw an exception
                            // because nc.News is in the base class
            .Map(a => a.MapKey("NewsId"));
    }
}

But caution: Be aware that NewsCommentView.News is not the other end of the relationship belonging to News.CommentViews. It means that if you have a NewsCommentView in your News.CommentViews collection then NewsCommentView.News does not point back to that News object. The other end is invisible and not exposed in the model. The mapping above just fixes the FK column name problem but doesn't change the relationships which conventions would create anyway (except maybe changing the relationship to required instead of optional).

Your SQL does not have an underscore between user and id.

Update EDMX from database (via right click menu) and check the mappings.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!