我一直在与流利,注释及其组合进行摔跤,但无法找出以下实体和上下文配置为何会生成具有重复外键的迁移。我已经阅读了所有其他帖子并尝试了所有建议的解决方案,每个解决方案似乎都是一个不同的组合,但我仍然无法让EF给我一个外键迁移。它总是产生这个额外的。
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会抛出异常。
我在这里做错了吗?
答案 0 :(得分:2)
使用它:
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; }
}