Как сконфигурировать множественные one-to-one отношения

У меня есть три сущности базы данных:

[Table("ReferencePhotos")]
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; }
}

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

DbReferencePhoto и DbDropPhoto используют DbSimpleLine. Мне бы хотелось, чтобы при добавлении нового DbDropPhoto у него был бы внешний ключ, указывающий на две DbSimpleLine (SimpleHorizontalLine, SimpleVerticalLine). Причем, DbDropPhoto изначально может быть создан и вовсе с SimpleHorizontalLineId и SimpleVerticalLineId равным null. DbReferencePhotoбудет иметь ссылку только на один DbSimpleLine соответственно. Чтобы сконфигурировать такую связь я использую fluentAPI:

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

У меня есть ощущение, что моя конфигурация может не совсем правильная, я взял за основу пример: http://csharpwavenet.blogspot.com/2013/06/multiple-foreign-keys-with-same-table.html

В частности, может это просто такой синтаксис, но меня смущает, что мне пришлось добавить ICollection<DbReferencePhoto> в DbSimpleLine и то, что мне пришлось написать .WithMany(a => a.ReferencePhoto), т.к. я бы на самом деле хотел, чтобы DbSimpleLine какая то одна имела связь с каким то одним ReferencePhoto.


Ответы (1 шт):

Автор решения: Fallingsappy

Сделал вот так:

       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; }
}
→ Ссылка