2017-02-06 6 views
0

私は流暢な注釈とその組み合わせに取り組んできましたが、次のエンティティとコンテキストの設定が重複した外部キ​​ーを持つ移行を生成する理由を見つけることができません。私は他のすべての記事を読んで、提案されたすべてのソリューションを試しましたが、それぞれが異なるコンボであるようですが、私はEFで1つの外部キーを使用して移行することはできません。常にこの余分なものを生成します。コンテキストEF6移行で生成された重複した外部キ​​ー

public class REODbContext:DbContext 
{ 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.ManyToManyCascadeDeleteConvention>(); 
     modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention>(); 

     modelBuilder.Entity<REOProperty>() 
      .HasKey(a => a.REOPropertyId) 
      .Property(a => a.REOPropertyId) 
      .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<PropertyPhotoUrl>() 
      .HasKey(a => a.PropertyPhotoUrlId) 
      .Property(a => a.PropertyPhotoUrlId) 
      .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity); 
     modelBuilder.Entity<PropertyPhotoUrl>() 
      .HasRequired(a => a.REOProperty) 
      .WithMany(a=>a.PropertyPhotoUrls) 
      .HasForeignKey(a => a.REOPropertyId); 
    } 
} 

public class REOProperty 
{ 
    public virtual ICollection<PropertyPhotoUrl> PropertyPhotoUrls { get; set; } 

    public Guid REOPropertyId { get; set; } 

} 
public class PropertyPhotoUrl 
{ 
    public Guid REOPropertyId { get; set; } 
    public virtual REOProperty REOProperty { get; set; } 

    public Guid PropertyPhotoUrlId { get; set; } 
    public string PhotoUrl { get; set; } 
} 

は子供 PropertyPhotoUrlエンティティに重複した外部キ​​ーでの移行を生成します。

CreateTable(
      "dbo.PropertyPhotoUrls", 
      c => new 
       { 
        PropertyPhotoUrlId = c.Guid(nullable: false, identity: true), 
        REOPropertyId = c.Guid(nullable: false), 
        PhotoUrl = c.String(), 
        REOProperty_REOPropertyId = c.Guid(), 
       }) 
      .PrimaryKey(t => t.PropertyPhotoUrlId) 
      .ForeignKey("dbo.REOProperties", t => t.REOPropertyId) 
      .ForeignKey("dbo.REOProperties", t => t.REOProperty_REOPropertyId) 
      .Index(t => t.REOPropertyId) 
      .Index(t => t.REOProperty_REOPropertyId); 

私はこの問題を以前に見てきましたが、通常、流暢なコードを変更することで解決できます。私が手動でマイグレーションを編集して私が望むものを得ようとすると、クエリを実行するときにefが例外をスローします。

ここで何か問題がありますか?

答えて

1

これを使用する:

public class REOProperty 
{ 
    [InverseProperty("REOProperty")] 
    public virtual ICollection<PropertyPhotoUrl> PropertyPhotoUrls { get; set; } 

    public Guid REOPropertyId { get; set; } 

} 

public class PropertyPhotoUrl 
{ 
    public Guid REOPropertyId { get; set; } 

    [ForeignKey("REOPropertyId")] 
    [InverseProperty("PropertyPhotoUrls")] 
    public virtual REOProperty REOProperty { get; set; } 

    public Guid PropertyPhotoUrlId { get; set; } 
    public string PhotoUrl { get; set; } 
} 
関連する問題