Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework getting confused about navigation property

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?

like image 545
d512 Avatar asked Apr 18 '15 18:04

d512


1 Answers

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; }
}
like image 155
Iraj Avatar answered Sep 17 '22 05:09

Iraj