配置多个一对一关系的正确方法是什么?

时间:2020-03-15 07:30:13

标签: c# entity-framework

我有一个实体DbDropPhoto,DbReferencePhoto和DbSimpleLine。 DbSimpleLine可以三种不同状态存储在数据库中:DropPhotoHorizo​​ntalLine,DropPhotoVerticalLine,DbReferencePhoto。这些状态中的每一个相互抵消。例如,作为DropPhotoVerticalLine的SimpleLine,会将DropPhotoHorizo​​ntalLine和DbReferencePhoto设置为null:

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

我正在尝试保存新的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();
        }
    }

当我这样做时,我会得到一个例外:

System.InvalidOperationException:'角色更改冲突 关系的“ DbDropPhoto_SimpleHorizo​​ntalLine_Target” 已检测到“ DDrop.Db.DbDropPhoto_SimpleHorizo​​ntalLine”。

1 个答案:

答案 0 :(得分:2)

当您具有同一类的多个实例时,应显式定义外键。这意味着:

[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; }
}
相关问题