流利的API产生重复的列



我们正在尝试重新配置我们的EF项目,以便我们使用流利的API配置而不是用于清洁数据模型的数据注释。我们有现有的桌子,现在我正在尝试验证我的努力。

这是旧的(部分)EF模型声明:

[Table("CustomerLocation")]
internal class CustomerLocationEF
{
    [Column(Order = 0), Key]
    public int CustomerID { get; set; }
    [Column(Order = 1), Key]
    [Required]
    [MaxLength(ModelConstants.MaxLength128)]
    [Index("IX_Customer_LocationReference", IsUnique = true)]
    public string LocationReference { get; set; }
    public virtual CustomerEF Customer { get; set; }
}
[Table("Customer")]
internal class CustomerEF
{
    [Key]
    public int CustomerID { get; set; }
    public virtual ICollection<CustomerLocationEF> Locations { get; set; }
}

这些工作正常并产生预期的模式。但是,这是新的型号和配置:

public class CustomerLocationModel
{
    public int CustomerId { get; set; }
    public string LocationReference { get; set; }
    public virtual CustomerModel Customer { get; set; }
}
public class CustomerModel
{
    public int Id { get; set; }
    public virtual ICollection<CustomerLocationModel> Locations { get; set; }
}
internal sealed class CustomerLocationTypeConfiguration : EntityTypeConfiguration<CustomerLocationModel>
{
    public CustomerLocationTypeConfiguration()
    {
        ToTable("CustomerLocation");
        HasKey(x => new {x.CustomerId, x.LocationReference});
        Property(x => x.CustomerId)
            .HasColumnName("CustomerID");
        HasRequired(x => x.Customer).WithMany().WillCascadeOnDelete(false);
    }
}

但是,这试图生成这样的表:

CreateTable(
        "dbo.CustomerLocation",
        c => new
            {
                CustomerID = c.Int(nullable: false),
                LocationReference = c.String(nullable: false, maxLength: 128),
                CustomerModel_Id = c.Int(),
            })
        .PrimaryKey(t => new { t.CustomerID, t.LocationReference })
        .ForeignKey("dbo.Customer", t => t.CustomerID)
        .ForeignKey("dbo.Customer", t => t.CustomerModel_Id)
        .Index(t => new { t.CustomerID, t.LocationReference }, unique: true, name: "IX_Customer_LocationReference")
        .Index(t => t.CustomerModel_Id);

注意重复列CustomerModel_Id和关联的外键。我以前遇到过这样的问题,并使用[ForeignKey]解决了这些问题,但是我是Fluent API的新手,并且不确定我在这里做错了什么。我如何流利地解决此问题,以便正确地拾取我的导航属性/外键?

事实证明,问题是在CustomerModel的映射配置中,一旦我清理了该问题,我的问题就消失了:

HasMany(x => x.Locations)
                .WithRequired(x => x.Customer)
                .HasForeignKey(x => x.CustomerId);

相关内容

  • 没有找到相关文章

最新更新