简体   繁体   English

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

[英]What is the proper way to configure multiple one-to-one relationship?

I have a Entities DbDropPhoto, DbReferencePhoto and DbSimpleLine.我有一个实体 DbDropPhoto、DbReferencePhoto 和 DbSimpleLine。 DbSimpleLine could be stored in database in three different states: DropPhotoHorizontalLine, DropPhotoVerticalLine, DbReferencePhoto. DbSimpleLine 可以以三种不同的状态存储在数据库中:DropPhotoHorizo​​ntalLine、DropPhotoVerticalLine、DbReferencePhoto。 Each of those states cancels each other.这些状态中的每一个都相互抵消。 For example SimpleLine that is DropPhotoVerticalLine, would have DropPhotoHorizontalLine and DbReferencePhoto set to null:例如,作为 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; }
}

my current configuration:我目前的配置:

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

I'm trying to save new dbSimpleLine:我正在尝试保存新的 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();
        }
    }

When i do, i get an exception:当我这样做时,我得到一个例外:

System.InvalidOperationException: 'Conflicting changes to the role 'DbDropPhoto_SimpleHorizontalLine_Target' of the relationship 'DDrop.Db.DbDropPhoto_SimpleHorizontalLine' have been detected.' System.InvalidOperationException: '已检测到关系'DDrop.Db.DbDropPhoto_SimpleHorizo​​ntalLine' 的角色'DbDropPhoto_SimpleHorizo​​ntalLine_Target' 的冲突更改。

You should explicitly define your foreign keys when you have multiple instance of the same class.当您有同一个类的多个实例时,您应该明确定义外键。 which means:意思是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM