2017-10-05 10 views
0

EFプロジェクトを再構成しようとしているため、よりクリーンなデータモデルにデータアノテーションの代わりに流暢なAPI設定を使用しています。私たちは既存のテーブルを持っており、今私の努力を検証しようとしています。Fluent API Duplicate Columnsを作成する

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

これらの作業罰金と予想されるスキーマを生成します。ここでは

は古い(部分)EFモデルの宣言です。しかし、ここに新しいモデルと構成は次のとおりです。

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と関連する外部キー:

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

しかし、これは、このようなテーブルを生成しようとします。これまでのような問題にデータアノテーションを付けて、[ForeignKey]で解決しましたが、私はFluent APIが新しく、ここで間違っていることがわかりません。 Fluentでこれを解決して、ナビゲーションプロパティ/外部キーを正しくピックアップする方法を教えてください。

答えて

0

それは私がそれをクリーンアップしたら、私の問題は去っていきました、問題はCustomerModelためのマッピング設定にあった判明:

HasMany(x => x.Locations) 
       .WithRequired(x => x.Customer) 
       .HasForeignKey(x => x.CustomerId); 
関連する問題