C# 配置多个一对一关系的正确方法是什么?
我有一个实体DbDropPhoto、DbReferencePhoto和DbSimpleLine。DbSimpleLine可以以三种不同的状态存储在数据库中:DropPhotoHorizontalLine、DropPhotoVerticalLine、DbReferencePhoto。这些州中的每一个都相互抵消。例如,作为DropPhotoVerticalLine的SimpleLine将DropPhotoHorizontalLine和DbReferencePhoto设置为null:C# 配置多个一对一关系的正确方法是什么?,c#,entity-framework,C#,Entity Framework,我有一个实体DbDropPhoto、DbReferencePhoto和DbSimpleLine。DbSimpleLine可以以三种不同的状态存储在数据库中:DropPhotoHorizontalLine、DropPhotoVerticalLine、DbReferencePhoto。这些州中的每一个都相互抵消。例如,作为DropPhotoVerticalLine的SimpleLine将DropPhotoHorizontalLine和DbReferencePhoto设置为null: [Table("
[Table("DropPhotos")]
public class DbDropPhoto
{
[Key]
public Guid DropPhotoId { get; set; }
public int ZDiameterInPixels { get; set; }
public virtual DbSimpleLine SimpleHorizontalLine { get; set; }
public virtual DbSimpleLine SimpleVerticalLine { get; set; }
}
public class DbReferencePhoto
{
[Key]
public Guid ReferencePhotoId { get; set; }
public virtual DbSimpleLine SimpleLine { get; set; }
}
[Table("SimpleLines")]
public class DbSimpleLine
{
[Key]
public Guid SimpleLineId { get; set; }
public virtual DbReferencePhoto ReferencePhoto { get; set; }
public virtual DbDropPhoto DropPhotoHorizontalLine { get; set; }
public virtual DbDropPhoto DropPhotoVerticalLine { get; set; }
}
我当前的配置:
modelBuilder.Entity<DbDropPhoto>()
.HasRequired(s => s.SimpleHorizontalLine)
.WithRequiredPrincipal(ad => ad.DropPhotoHorizontalLine);
modelBuilder.Entity<DbDropPhoto>()
.HasRequired(s => s.SimpleVerticalLine)
.WithRequiredPrincipal(ad => ad.DropPhotoVerticalLine);
modelBuilder.Entity<DbReferencePhoto>()
.HasRequired(s => s.SimpleLine)
.WithRequiredPrincipal(ad => ad.ReferencePhoto);
modelBuilder.Entity()
.HasRequired(s=>s.SimpleHorizontalLine)
.具有所需的委托人(ad=>ad.DropPhotoHorizontalLine);
modelBuilder.Entity()
.HasRequired(s=>s.SimpleVerticalLine)
.具有所需的委托人(ad=>ad.DropPhotoVerticalLine);
modelBuilder.Entity()
.HasRequired(s=>s.SimpleLine)
.具有所需的主体(ad=>ad.ReferencePhoto);
我正在努力拯救新的dbSimpleLine:
public async Task CreateOrUpdateSimpleLine(List<DbSimpleLine> dbSimpleLines)
{
using (var context = new DDropContext())
{
foreach (var dbSimpleLine in dbSimpleLines)
{
var dbSimpleLineToUpdate = await context.SimpleLines.FirstOrDefaultAsync(x => x.SimpleLineId == dbSimpleLine.SimpleLineId);
if (dbSimpleLineToUpdate != null)
{
try
{
context.Entry(dbSimpleLineToUpdate).CurrentValues.SetValues(dbSimpleLine);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
else
{
context.SimpleLines.Add(dbSimpleLine);
}
}
await context.SaveChangesAsync();
}
}
公共异步任务CreateOrUpdateSimpleLine(列表dbSimpleLines)
{
使用(var context=new DDropContext())
{
foreach(dbSimpleLines中的var dbSimpleLine)
{
var dbSimpleLineToUpdate=wait context.SimpleLines.FirstOrDefaultAsync(x=>x.SimpleLineId==dbSimpleLine.SimpleLineId);
if(dbSimpleLineToUpdate!=null)
{
尝试
{
context.Entry(dbSimpleLineToUpdate).CurrentValues.SetValues(dbSimpleLine);
}
捕获(例外e)
{
抛出新异常(e.Message);
}
}
其他的
{
context.simpleline.Add(dbSimpleLine);
}
}
wait context.saveChangesSync();
}
}
当我这样做时,我会得到一个例外:
System.InvalidOperationException:'对角色的更改冲突
关系的“DbDropPhoto\u SimpleHorizontalLine\u Target”
已检测到“DDrop.Db.DbDropPhoto_SimpleHorizontalLine”
当具有同一类的多个实例时,应该显式定义外键。这意味着:
[Table("DropPhotos")]
public class DbDropPhoto
{
[Key]
public Guid DropPhotoId { get; set; }
public int ZDiameterInPixels { get; set; }
public int? SimpleHorizontalLineId { get; set; }
[ForeignKey("SimpleHorizontalLineId")]
public virtual DbSimpleLine SimpleHorizontalLine { get; set; }
public int? SimpleVerticalLineId { get; set; }
[ForeignKey("SimpleVerticalLineId")]
public virtual DbSimpleLine SimpleVerticalLine { get; set; }
}
public class DbReferencePhoto
{
[Key]
public Guid ReferencePhotoId { get; set; }
public Guid SimpleLineId { get; set; }
[ForeignKey("SimpleLineId")]
public virtual DbSimpleLine SimpleLine { get; set; }
}
[Table("SimpleLines")]
public class DbSimpleLine
{
[Key]
public Guid SimpleLineId { get; set; }
public int? ReferencePhotoId { get; set;}
[ForeignKey("ReferencePhotoId")]
public virtual DbReferencePhoto ReferencePhoto { get; set; }
public int? DropPhotoHorizontalLineId { get; set;}
[ForeignKey("DropPhotoHorizontalLineId")]
public virtual DbDropPhoto DropPhotoHorizontalLine { get; set; }
public int? DropPhotoVerticalLineId { get; set;}
[ForeignKey("DropPhotoVerticalLineId")]
public virtual DbDropPhoto DropPhotoVerticalLine { get; set; }
}
下面介绍如何使用fluent api执行此操作: