EF6 Неспособность строить модель для таблицы Split / Shared Primary Key + базовый класс?
Вопрос
<Сильная> проблема
Я пытаюсь поделиться большим столом (200 + поля) около ~ 7 объектов, используя расщепление таблицы в соответствии с
Решение
В конце концов я решил принять ручное решение, поскольку я не мог получить никаких попыток улучшения для работы.
Код и модели не элегантны, но в конце дня он выполняет нормально.Я внедрил шаблон в 3 области, и он выполняет как требуется, в домене и на уровне SQL.
Чтобы облегчить боль и предоставить разработчикам последовательным способом работать с этим шаблоном, я создал этот интерфейс для обеспечения соблюдения всех отношений:
public interface ISharedFranchiseId
{
FranchiseBilling Billing { get; set; }
FranchiseCompliance Compliance { get; set; }
FranchiseLeadAllocation LeadAllocation { get; set; }
FranchiseMessaging Messaging { get; set; }
FranchiseMiscellaneous Miscellaneous { get; set; }
FranchiseSignup Signup { get; set; }
}
.
Таким образом, каждый из моделей совместно использует первичный ключ, имеет эти свойства (раздражающий бит):
public class FranchiseBilling/Compliance/etc : ISharedFranchiseId
{
// Properties implemented on this model
#region Navigations to other entities sharing primary key
public virtual FranchiseBilling Billing { get; set; }
public virtual FranchiseCompliance Compliance { get; set; }
public virtual FranchiseLeadAllocation LeadAllocation { get; set; }
public virtual FranchiseMessaging Messaging { get; set; }
public virtual FranchiseMiscellaneous Miscellaneous { get; set; }
public virtual FranchiseSignup Signup { get; set; }
#endregion
}
.
и настроить через беглый API следующим образом (болезненный бит):
// Franchise = the "primary/parent" model
public class FranchiseMapping : EntityTypeConfiguration<Franchise>
{
public FranchiseMapping()
{
HasRequired(x => x.Billing).WithRequiredPrincipal();
HasRequired(x => x.Compliance).WithRequiredPrincipal();
HasRequired(x => x.LeadAllocation).WithRequiredPrincipal();
HasRequired(x => x.Miscellaneous).WithRequiredPrincipal();
HasRequired(x => x.Messaging).WithRequiredPrincipal();
HasRequired(x => x.Signup).WithRequiredPrincipal();
}
}
// Now each "child" model gets link to all the others. We only need links going one way,
// So each model links to the ones listed below.
// This makes it easy to implement an extra child model down the track as we just
// insert the configuration it here and copy from the next one.
public class FranchiseBillingMapping : EntityTypeConfiguration<FranchiseBilling>
{
public FranchiseBillingMapping()
{
Ignore(x => x.Billing);
HasRequired(x => x.Compliance).WithRequiredDependent(x => x.Billing);
HasRequired(x => x.LeadAllocation).WithRequiredPrincipal(x => x.Billing);
HasRequired(x => x.Miscellaneous).WithRequiredPrincipal(x => x.Billing);
HasRequired(x => x.Messaging).WithRequiredPrincipal(x => x.Billing);
HasRequired(x => x.Signup).WithRequiredPrincipal(x => x.Billing);
}
}
public class FranchiseComplianceMapping : EntityTypeConfiguration<FranchiseCompliance>
{
public FranchiseComplianceMapping()
{
Ignore(x => x.Compliance);
HasRequired(x => x.LeadAllocation).WithRequiredPrincipal(x => x.Compliance);
HasRequired(x => x.Miscellaneous).WithRequiredPrincipal(x => x.Compliance);
HasRequired(x => x.Messaging).WithRequiredPrincipal(x => x.Compliance);
HasRequired(x => x.Signup).WithRequiredPrincipal(x => x.Compliance);
}
}
public class FranchiseLeadAllocationMapping : EntityTypeConfiguration<FranchiseLeadAllocation>
{
public FranchiseLeadAllocationMapping()
{
Ignore(x => x.LeadAllocation);
HasRequired(x => x.Miscellaneous).WithRequiredPrincipal(x => x.LeadAllocation);
HasRequired(x => x.Messaging).WithRequiredPrincipal(x => x.LeadAllocation);
HasRequired(x => x.Signup).WithRequiredPrincipal(x => x.LeadAllocation);
}
}
public class FranchiseeMiscellaneousMapping : EntityTypeConfiguration<FranchiseeMiscellaneous>
{
public FranchiseeMiscellaneousMapping()
{
Ignore(x => x.Miscellaneous);
HasRequired(x => x.Messaging).WithRequiredPrincipal(x => x.Miscellaneous);
HasRequired(x => x.Signup).WithRequiredPrincipal(x => x.Miscellaneous);
}
}
public class FranchiseMessagingMapping : EntityTypeConfiguration<FranchiseMessaging>
{
public FranchiseMessagingMapping()
{
Ignore(x => x.Messaging);
HasRequired(x => x.Signup).WithRequiredPrincipal(x => x.Messaging);
}
}
public class FranchiseSignupMapping : EntityTypeConfiguration<FranchiseSignup>
{
public FranchiseSignupMapping()
{
Ignore(x => x.Signup);
}
}
.