【问题标题】:Entity Framework Double relation between tables实体框架表之间的双重关系
【发布时间】:2018-11-13 23:55:03
【问题描述】:

我正在使用 Entity Framework 开发一个 ASP .Net CORE MVC 网站,并且我在两个表之间有双重关系:

文档类型
#DocTypeName
#DocTypeValidationText

翻译界面
+id

DocTypeName 和 DocTypeValidationText 是 TraductionInterface 表的 ForeignKey:一个 DocType 有 2 个 TraductionInterface

这是我的课:

DocType.cs

 public class DocType
{
    [Key]
    public int DocTypeID { get; set; }

    [Required]
    public int DocTypeName { get; set; }
    [ForeignKey("DocTypeName")]
    public virtual TraductionInterface TraductionInterfaceName { get; set; }

    [Required]
    public int DocTypeValidationText { get; set; }
    [ForeignKey("DocTypeValidationText")]
    public virtual TraductionInterface TraductionInterfacevalidationText { get; set; }
}

TraductionInterface.cs

public class TraductionInterface
    {
        [Key]
        public int TraductionInterfaceID;

        [ForeignKey("DocTypeName")]
        public virtual DocType DocTypeName { get; set; }

        [ForeignKey("DocTypeValidationText")]
        public virtual DocType DocTypeValidationText { get; set; }

     }

ApplicationDbContext.cs

builder.Entity<DocType>().ToTable("DocType").HasOne(d => d.TraductionInterfaceName).WithOne(t => t.DocTypeName);
            builder.Entity<DocType>().HasOne(d => d.TraductionInterfacevalidationText).WithOne(t => t.DocTypeValidationText);

但是当我尝试更新时出现此错误

无法为实体类型“TraductionInterface”上的属性“DocTypeValidationText”调用属性,因为它被配置为导航属性。属性只能用于配置标量属性。

编辑
@user1672994 的解决方案似乎已经解决了问题,但只有第一个关系适用:
如果
builder.Entity&lt;DocType&gt;().ToTable("DocType").HasOne(d =&gt; d.TraductionInterfaceName).WithOne(t =&gt; t.DocTypeName);
之前是
builder.Entity&lt;DocType&gt;().HasOne(d =&gt; d.TraductionInterfacevalidationText).WithOne(t =&gt; t.DocTypeValidationText);
TraductionInterfaceName 关系适用,否则它是 TraductionInterfacevalidationText 关系。我怎样才能使这两个关系一起工作?

【问题讨论】:

  • 如果您不想暴露外键 ID,只需删除 [ForeignKey] 属性,EF 将在幕后创建它们。就个人而言,我更喜欢公开它们。

标签: c# asp.net entity-framework ef-code-first asp.net-core-mvc


【解决方案1】:

错误本身表明您应该定义标量属性并同时定义ForeignKey 关系。下面的代码定义了关系属性并在附加ForeignKey关系时使用它

public class TraductionInterface
{
    [Key]
    public int TraductionInterfaceID {get; set; }

    public int DocTypeNameId {get; set; }

    [ForeignKey("DocTypeNameId")]
    public virtual DocType DocTypeName { get; set; }

    public int DocTypeValidationTextId {get; set; }

    [ForeignKey("DocTypeValidationTextId")]
    public virtual DocType DocTypeValidationText { get; set; }

 } 

【讨论】:

  • 您的解决方案似乎修复了错误,但现在我遇到了另一个错误:实体类型“TraductionInterface”需要定义主键。但是 TraductionInterfaceID 作为注释 [Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  • 设置密钥为builder.Entity&lt;TraductionInterface&gt;().ToTable("DocType").HasKey(t =&gt; t.TraductionInterfaceID)
  • 我试过了,但得到了错误:The properties expression 't =&gt; Convert(t.TraductionInterfaceID, Object)' is not valid. The expression should represent a property access: 't =&gt; t.MyProperty'. When specifying multiple properties use an anonymous type: 't =&gt; new { t.MyProperty1, t.MyProperty2 }'. ps:它是到 table("TraductionInterface") 而不是 ToTable("DocType") 还是我错了?
  • 你是对的,应该是table("TraductionInterface")
  • 在定义实体时检查是否必须定义HasForeignKey
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多