我正在使用Entity Framework 6.1.1,我有一个用户表和一个 User_Documents 表(1 :许多)。我已经从 User_Documents 到用户的导航属性,事情正常。
public partial class User_Document { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public long User_Document_ID {get;组; } public long User_ID {get;组; } [ForeignKey(User_ID)] public virtual User User {get;组; }我将导航属性从Users添加到User_Documents
public partial class User { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public long User_ID {get;组; } [StringLength(50)] public string Username {get;组; } public virtual List< User_Document>文件{get;组; } }现在我尝试运行应用程序时收到错误:
System.Data.Entity.ModelConfiguration.ModelValidationException:在模型生成期间检测到一个或更多的验证错误: p>
User_Documents:Name:EntityContainer中的每个成员名称必须为 unique。已经定义了一个名为User_Documents的成员。
当然有一个表 User_Documents 但没有该名称的其他属性。我不知道它是什么困惑的。也许它取名称用户和属性名称文档,并尝试创建一个名为User_Documents的东西?如果我将它从文件 重命名为 Some_Documents 像这样
public virtual List< User_Document> Some_Documents {get;组;然后我收到一个不同的错误说明
System.InvalidOperationException:支持'PipeTrackerContext'上下文的模型已经更改,因为数据库是创建的。考虑使用代码优先迁移来更新数据库
所以我运行添加迁移我得到这个:
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); }为什么要添加一个新列,名为 User_User_ID ?为什么我不能像我想要的那样添加文档导航属性?
解决方案使用InverseProperty这样:
public partial class User_Document { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public long User_Document_ID {get;组; } public long User_ID {get;组; } [ForeignKey(User_ID)] [InverseProperty(Documents)] public virtual User User {get;组; }And:
public partial class User { [Key] [DatabaseGenerated(DatabaseGeneratedOption.None)] public long User_ID {get;组; } [StringLength(50)] public string Username {get;组; } [InverseProperty(User)] public virtual List< User_Document>文件{get;组; } }
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?
解决方案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; } }
更多推荐
实体框架对导航属性感到困惑
发布评论