C# 如何更新具有多个一对一关系的实体
我有实体:DbDropPhoto、DbReferencePhoto和DbSimpleLine。DbSimpleLines由bReferencePhoto和DbDropPhoto使用。我想使用每个DbSimpleLine的实体使DbSimpleLine只能链接到其中一个。例如,如果DbSimpleLine是DropPhotoHorizontalLine,则其他两个属性将为null。我搜索了一些解决方案,找到了以下解决方案: 但现在我真的很困惑。例如,根据示例,我的DbSimpleLine类应该有ReferencePhoto集合,但实际上一个DbSimpleLine只能有一个ReferencePhoto。配置关系时我做错了什么?对于关系配置,我使用fluent API:C# 如何更新具有多个一对一关系的实体,c#,entity-framework,C#,Entity Framework,我有实体:DbDropPhoto、DbReferencePhoto和DbSimpleLine。DbSimpleLines由bReferencePhoto和DbDropPhoto使用。我想使用每个DbSimpleLine的实体使DbSimpleLine只能链接到其中一个。例如,如果DbSimpleLine是DropPhotoHorizontalLine,则其他两个属性将为null。我搜索了一些解决方案,找到了以下解决方案: 但现在我真的很困惑。例如,根据示例,我的DbSimpleLine类应该有R
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(b => b.SimpleHorizontalLine)
.WithMany(a => a.DropPhotoHorizontalLine)
.HasForeignKey(b => b.SimpleHorizontalLineId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(b => b.SimpleVerticalLine)
.WithMany(a => a.DropPhotoVerticalLine)
.HasForeignKey(b => b.SimpleVerticalLineId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<DbReferencePhoto>()
.HasOptional(b => b.SimpleReferencePhotoLine)
.WithMany(a => a.ReferencePhoto)
.HasForeignKey(b => b.SimpleReferencePhotoLineId)
.WillCascadeOnDelete(false);
结果是这样做的:
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(c => c.SimpleHorizontalLine)
.WithMany()
.HasForeignKey(s => s.SimpleHorizontalLineId);
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(c => c.SimpleVerticalLine)
.WithMany()
.HasForeignKey(s => s.SimpleVerticalLineId);
modelBuilder.Entity<DbReferencePhoto>()
.HasOptional(c => c.SimpleReferencePhotoLine)
.WithMany()
.HasForeignKey(s => s.SimpleReferencePhotoLineId);
[Table("SimpleLines")]
public class DbSimpleLine
{
[Key]
public Guid SimpleLineId { get; set; }
}
[Table("ReferencePhotos")]
public class DbReferencePhoto
{
[Key]
public Guid ReferencePhotoId { get; set; }
public Guid? SimpleReferencePhotoLineId { get; set; }
[ForeignKey("SimpleReferencePhotoLineId")]
public virtual DbSimpleLine SimpleReferencePhotoLine { get; set; }
}
[Table("DropPhotos")]
public class DbDropPhoto
{
[Key]
public Guid DropPhotoId { get; set; }
public Guid? SimpleHorizontalLineId { get; set; }
[ForeignKey("SimpleHorizontalLineId")]
public virtual DbSimpleLine SimpleHorizontalLine { get; set; }
public Guid? SimpleVerticalLineId { get; set; }
[ForeignKey("SimpleVerticalLineId")]
public virtual DbSimpleLine SimpleVerticalLine { get; set; }
}
modelBuilder.Entity()
.has可选(c=>c.SimpleHorizontalLine)
.有很多
.HasForeignKey(s=>s.SimpleHorizontalLineId);
modelBuilder.Entity()
.HAS可选(c=>c.SimpleVerticalLine)
.有很多
.HasForeignKey(s=>s.simpleverticalineid);
modelBuilder.Entity()
.has可选(c=>c.SimpleReferencePhotoLine)
.有很多
.HasForeignKey(s=>s.SimpleReferencePhotoLineId);
[表(“简单线”)]
公共类数据库
{
[关键]
公共Guid SimpleLineId{get;set;}
}
[表(“参考照片”)]
公共类DbReferencePhoto
{
[关键]
公共Guid ReferencePhotoId{get;set;}
公共Guid?SimpleReferencePhotoLineId{get;set;}
[外键(“SimpleReferencePhotoLineId”)]
公共虚拟DbSimpleLine SimpleReferencePhotoLine{get;set;}
}
[表格(“DropPhotos”)]
公共类照片
{
[关键]
公共Guid DropPhotoId{get;set;}
公共Guid?SimpleHorizontalLineId{get;set;}
[外键(“SimpleHorizontalLineId”)]
公共虚拟数据库impleline SimpleHorizontalLine{get;set;}
公共Guid?SimpleVerticalLineId{get;set;}
[ForeignKey(“SimpleVerticalLineId”)]
公共虚拟DbSimpleLine SimpleVerticalLine{get;set;}
}
public async Task UpdatDropPhoto(DbDropPhoto dropPhoto)
{
using (var context = new DDropContext())
{
var dropPhotoToUpdate = await context.DropPhotos.FirstOrDefaultAsync(x => x.DropPhotoId == dropPhoto.DropPhotoId);
try
{
if (dropPhoto.SimpleVerticalLine != null)
context.SimpleLines.Add(dropPhoto.SimpleVerticalLine);
if (dropPhoto.SimpleHorizontalLine != null)
context.SimpleLines.Add(dropPhoto.SimpleHorizontalLine);
context.Entry(dropPhotoToUpdate).CurrentValues.SetValues(dropPhoto);
await context.SaveChangesAsync();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
await context.SaveChangesAsync();
}
}
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(c => c.SimpleHorizontalLine)
.WithMany()
.HasForeignKey(s => s.SimpleHorizontalLineId);
modelBuilder.Entity<DbDropPhoto>()
.HasOptional(c => c.SimpleVerticalLine)
.WithMany()
.HasForeignKey(s => s.SimpleVerticalLineId);
modelBuilder.Entity<DbReferencePhoto>()
.HasOptional(c => c.SimpleReferencePhotoLine)
.WithMany()
.HasForeignKey(s => s.SimpleReferencePhotoLineId);
[Table("SimpleLines")]
public class DbSimpleLine
{
[Key]
public Guid SimpleLineId { get; set; }
}
[Table("ReferencePhotos")]
public class DbReferencePhoto
{
[Key]
public Guid ReferencePhotoId { get; set; }
public Guid? SimpleReferencePhotoLineId { get; set; }
[ForeignKey("SimpleReferencePhotoLineId")]
public virtual DbSimpleLine SimpleReferencePhotoLine { get; set; }
}
[Table("DropPhotos")]
public class DbDropPhoto
{
[Key]
public Guid DropPhotoId { get; set; }
public Guid? SimpleHorizontalLineId { get; set; }
[ForeignKey("SimpleHorizontalLineId")]
public virtual DbSimpleLine SimpleHorizontalLine { get; set; }
public Guid? SimpleVerticalLineId { get; set; }
[ForeignKey("SimpleVerticalLineId")]
public virtual DbSimpleLine SimpleVerticalLine { get; set; }
}