gpt4 book ai didi

c# - EF 5 代码首先生成额外的外键

转载 作者:行者123 更新时间:2023-11-30 18:25:08 24 4
gpt4 key购买 nike

有时 EF 会在生成的表中包含 *_Id 外键,即使我指定了外键也是如此。

类:

public partial class Product : IFoo, IBar
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[XmlIgnore, ScriptIgnore, ForeignKey("InsuranceClassId"), Display(Name = "Insurance Class")]
public virtual InsuranceClass InsuranceClass { get; set; }
[Display(Name = "Insurance Class")]
public int InsuranceClassId { get; set; }

[XmlIgnore, ScriptIgnore, ForeignKey("ProductGroupId"), Display(Name = "Product Group")]
public virtual ProductGroup ProductGroup { get; set; }
[Display(Name = "Product Group")]
public int ProductGroupId { get; set; }

//[Snip]
}

public partial class InsuranceClass : IFoo, IBar
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[MaxLength(255), Display(Name = "Description"), Required(AllowEmptyStrings = false)]
public string Description { get; set; }

[XmlIgnore, ScriptIgnore]
public virtual List<Product> Products { get; set; }

public InsuranceClass()
{
this.Products = new List<Product>();
}

//[Snip]
}

但我的 Products 表最终是这样的:

Products
Id
InsuranceClassId
ProductGroupId
InsuranceClass_Id
ProductGroup_Id

我做错了什么?它只发生在少数几张 table 上。

最佳答案

不是真正的答案,但上次发生在我身上时,我有错误的映射,例如 HasOptional(xx).WithMany();(缺少 .HasForeignKey(xx)) 和 EF 约定在另一个 xx_Id 列中启动 - 也许你应该检查一下。

关于c# - EF 5 代码首先生成额外的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30457718/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com