实体框架对导航属性感到困惑 [英] Entity Framework getting confused about navigation property

查看:162
本文介绍了实体框架对导航属性感到困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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;组; 

然后我收到一个不同的错误说明



< blockquote>

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; }
}

这篇关于实体框架对导航属性感到困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆