У меня есть таблица с именем SystemAccount
, которая (до недавнего времени) имела на ней MasterAccountID
, которая указывала бы на ее родительскую учетную запись (очевидно,). Мой клиент теперь сказал мне, что иногда бывает, что у учетной записи могут быть две родительские учетные записи (у них никогда не было больше). Я пытаюсь выполнить настройку в классе SystemAccount
, но это не создает отношения, которые я хочу.Два отношения «один ко многим» в одной таблице
Вот часть кода класса:
[ForeignKey("MasterAccount")]
public int? MasterAccountID { get; set; }
[ForeignKey("SecondMasterAccount")]
public int? SecondMasterAccountID { get; set; }
public virtual SystemAccount MasterAccount { get; set; }
public virtual SystemAccount SecondMasterAccount { get; set; }
public virtual List<SystemAccount> AllSubAccounts { get; set; }
public virtual List<SystemAccount> SecondarySubAccounts { get; set; }
Когда я делаю это я получаю 4 FKs в таблице, 2 из которых генерируется автоматически (SystemAccount_ID
и SystemAccount_ID1
). Я даже попытался поместить атрибут [InverseProperty]
на MasterAccount
и SecondMasterAccount
, чтобы указать на списки, и это дает мне ошибку каждый раз (EDIT: Это дает мне NullReferenceException
).
Я знаю, что я должен превратить его в многие-ко-многим, но я столкнулся срок в ближайшее время, и рефакторинга использования MasterAccount
и MasterAccountID
бы меня далеко за пределы срока.
Как я могу заставить это работать?
EDIT: Исключение трассировки стека:
System.NullReferenceException was unhandled by user code
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=EntityFramework
StackTrace:
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EdmEntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(EdmModel model)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context)
at System.Data.Entity.Database.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c)
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Database.Initialize(Boolean force)
at Tests.Core.UI.SessionStartTests.ShouldSuccessfullyInitializeDatabase() in c:\Projects\Current\tests\Tests.Core\UI\StartTests.cs:line 72
InnerException:
EDIT 2: Когда я использовал предложение мохо в:
System.Data.Entity.ModelConfiguration.ModelValidationException : One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'SystemAccount_AllSubAccounts_Target' in relationship 'SystemAccount_AllSubAccounts'. Valid values for multiplicity for the Principal Role are '0..1' or '1'.
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'SystemAccount_AllSubAccounts_Source' in relationship 'SystemAccount_AllSubAccounts'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'SystemAccount_SecondarySubAccounts_Target' in relationship 'SystemAccount_SecondarySubAccounts'. Valid values for multiplicity for the Principal Role are '0..1' or '1'.
\tSystem.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role 'SystemAccount_SecondarySubAccounts_Source' in relationship 'SystemAccount_SecondarySubAccounts'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
EDIT 3: Мой код для обновления базы данных:
Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, Configuration>());
var db = new MyDbContext();
db.Database.Initialize(true);
Мой OnModelCreating
метод:
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Entity<ClientStatisticsView>().ToTable("ClientStatistics");
base.OnModelCreating(modelBuilder);
Мой Configuration
файл:
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(MyDbContext context)
{
}
Я предполагаю, что код происходит от системной учетной записи, вы можете разместить основную учетную запись, то и соотношение между этими двумя – Gustavo
Основная учетная запись поступает из той же таблицы. Это само по себе. – IronMan84
Какова ошибка, возникающая при использовании атрибута '[InverseProperty]'? (Этот атрибут был бы моим предложением решить проблему ...) – Slauma