5

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?

d512
  • 32,267
  • 28
  • 81
  • 107

2 Answers2

5

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; }
}
Iraj
  • 1,492
  • 5
  • 18
  • 42
0

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.

Bilal Fazlani
  • 6,727
  • 9
  • 44
  • 90
  • Right, but if I rename Documents to UserDocuments, it still wants to add a field called User_User_ID to the Users table. Why is that? – d512 Apr 18 '15 at 19:27
  • Nope. Adding of User_User_ID is related to the foreign key attribute. Do you have the attribute or you have removed it ? – Bilal Fazlani Apr 18 '15 at 19:28
  • It looks just like the code I posted. The User property in the User_Documents table does have the [ForeignKey] attribute on it. – d512 Apr 18 '15 at 19:33