【问题标题】:Fluent API for navigation property in the two waysFluent API 用于导航属性的两种方式
【发布时间】:2012-03-16 21:29:22
【问题描述】:

public class ProductType
{
    public Guid ID { get; set }
    public string Name { get; set }
    public ICollection<ProductCategory> Categories { get; set }
}

public class ProductCategory
{
    public Guid ID { get; set }
    public string Name { get; set }
    public ProductType ProductType { get; set; }
}

配置

产品类型配置

HasMany(p => p.Categories).WithRequired().WillCascadeOnDelete(false);

问题

注意属性CategoriesProductType

关系是一对多 (ProductType) 的关系 (ProductCategory),但是,ProductCategory 与单个 ProductType 相关联!

在我的数据库中,它正在创建两个 FKS! 对于这种情况如何配置(使用 FluentAPI)??

谢谢!!!

【问题讨论】:

    标签: entity-framework-4 ef-code-first fluent fluent-interface


    【解决方案1】:
    HasMany(p => p.Categories)
        .WithRequired(c => c.ProductType) // specify inverse navigation property here
        .WillCascadeOnDelete(false);
    

    如果您省略 WithRequired 中导航属性的 lambda,则 EF 假定 Category.ProductType 属于另一个第二个关系 - 这就是数据库表中第二个外键的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多