[英]How to create one-to-one relationship in Entity Framework Core?
[英]How to Update Entity with multiple one-to-one relationship
我有实体:DbDropPhoto、DbReferencePhoto 和 DbSimpleLine。 DbSimpleLines 由 bReferencePhoto 和 DbDropPhoto 使用。 我想使用每个 DbSimpleLine 的实体使 DbSimpleLine 只能链接到其中一个。 例如,如果 DbSimpleLine 是 DropPhotoHorizontalLine,则其他两个属性将为空。 我搜索了一些解决方案并找到了这个: http : //csharpwavenet.blogspot.com/2013/06/multiple-foreign-keys-with-same-table.html但现在我真的很困惑。 例如,根据示例,我的 DbSimpleLine 类应该有 ReferencePhoto 的集合,但实际上一个 DbSimpleLine 只能有一个 ReferencePhoto。 我在配置关系时做错了什么? 对于关系配置,我使用 fluent API:
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);
.WithMany() 让我很困惑,因为 SimpleVerticalLine(例如)不应该有很多 DropPhoto。
[Table("SimpleLines")]
public class DbSimpleLine
{
[Key]
public Guid SimpleLineId { get; set; }
public ICollection<DbReferencePhoto> ReferencePhoto { get; set; }
public ICollection<DbDropPhoto> DropPhotoHorizontalLine { get; set; }
public ICollection<DbDropPhoto> DropPhotoVerticalLine { get; set; }
}
public class DbReferencePhoto
{
[Key]
public Guid ReferencePhotoId { get; set; }
public Guid? SimpleReferencePhotoLineId { get; set; }
public DbSimpleLine SimpleReferencePhotoLine { get; set; }
}
[Table("DropPhotos")]
public class DbDropPhoto
{
[Key]
public Guid DropPhotoId { get; set; }
public Guid? SimpleHorizontalLineId { get; set; }
public DbSimpleLine SimpleHorizontalLine { get; set; }
public Guid? SimpleVerticalLineId { get; set; }
public DbSimpleLine SimpleVerticalLine { get; set; }
}
此外,当我尝试将 dbDropPhoto 保存到数据库时,出现异常:
System.InvalidOperationException: '违反了多重性约束。 关系“DDrop.Db.DbDropPhoto_Drop”的角色“DbDropPhoto_Drop_Target”具有多重性1或0..1。
保存方法的代码:
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.