2017-02-06 3 views
0

Я боролся с беглостью, аннотациями и их комбинациями, но не могу понять, почему следующие сущности и контекстная конфигурация генерируют миграцию с дублированным внешним ключом. Я прочитал все другие сообщения и попробовал все предлагаемые решения, каждый из которых, похоже, отличается от других, но я все еще не могу заставить EF перенаправить меня с одним внешним ключом. Он всегда генерирует этот дополнительный.Повторяющиеся внешние ключи, сгенерированные при миграции EF6

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

с контекстом

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

Формирует миграцию с повторяющимися внешними ключами на ребенок 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; } 
} 
Смежные вопросы