简体   繁体   中英

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

I have a Entities DbDropPhoto, DbReferencePhoto and DbSimpleLine. DbSimpleLine could be stored in database in three different states: DropPhotoHorizontalLine, DropPhotoVerticalLine, DbReferencePhoto. Each of those states cancels each other. For example SimpleLine that is DropPhotoVerticalLine, would have DropPhotoHorizontalLine and DbReferencePhoto set to 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:

    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.'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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