Entity Framework getting confused about navigation property

折月煮酒 提交于 2019-12-23 10:25:54

问题


I'm using Entity Framework 6.1.1 and I have a Users table and a User_Documents table (1:many). I already had a navigation property from User_Documents to User were things were working fine.

public partial class User_Document
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long User_Document_ID { get; set; }

    public long User_ID { get; set; }

    [ForeignKey("User_ID")]
    public virtual User User { get; set; }
}

I added a navigation property from Users to User_Documents

public partial class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long User_ID { get; set; }

    [StringLength(50)]
    public string Username { get; set; }

    public virtual List<User_Document> Documents { get; set; }
}

and now I'm getting an error when I try to run the application:

System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:

User_Documents: Name: Each member name in an EntityContainer must be unique. A member with name 'User_Documents' is already defined.

Of course there is a table called User_Documents but no other property with that name. I'm not sure what's it getting confused by. Maybe it's taking the table name "User" and the property name "Documents" and trying to create something called "User_Documents" out of it? If I rename it to from Documents to Some_Documents like this

public virtual List<User_Document> Some_Documents { get; set; }

then I get a different error stating

System.InvalidOperationException: The model backing the 'PipeTrackerContext' context has changed since the database was created. Consider using Code First Migrations to update the database

So I run Add-Migration and I get this:

public override void Up()
{
    AddColumn("dbo.User_Documents", "User_User_ID", c => c.Long());
    CreateIndex("dbo.User_Documents", "User_User_ID");
    AddForeignKey("dbo.User_Documents", "User_User_ID", "dbo.Users", "User_ID");
}

Why is it trying to add a new column called User_User_ID? Why can't I just add the Document navigation property like I want?


回答1:


use InverseProperty Like this :

public partial class User_Document
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long User_Document_ID { get; set; }

    public long User_ID { get; set; }

    [ForeignKey("User_ID")]
    [InverseProperty("Documents")]
    public virtual User User { get; set; }
}

And :

public partial class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public long User_ID { get; set; }

    [StringLength(50)]
    public string Username { get; set; }

    [InverseProperty("User")]
    public virtual List<User_Document> Documents { get; set; }
}



回答2:


Why is it trying to add a new column called User_User_ID? Why can't I just add the Document navigation property like I want?

By convention, it will create the foreign key as tablename_columnName which is User_User_ID. This happens when you remove the [ForeignKey("User_ID")] attribute OR don't have foreign key property.

If you change the property name Documents to something else (likeUserDocuments) you wont face this conflict of names.



来源:https://stackoverflow.com/questions/29721471/entity-framework-getting-confused-about-navigation-property

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