gpt4 book ai didi

c# - 同一张表中的两个一对多关系

转载 作者:行者123 更新时间:2023-11-30 13:57:19 24 4
gpt4 key购买 nike

我有一个名为 SystemAccount 的表,它(直到最近)有一个 MasterAccountID 指向它的父帐户(显然是一个 int? )。我的客户现在告诉我,有时一个帐户可以有 2 个父帐户(没有一个比这更多)。我一直在尝试在我的 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 个 FK,其中 2 个是自动生成的(SystemAccount_IDSystemAccount_ID1)。我什至尝试将 [InverseProperty] 属性放在 MasterAccountSecondMasterAccount 上以指向列表,它分别给我一个错误时间(编辑:它给了我一个 NullReferenceException)。

我知道我应该将它变成多对多关系,但我很快就要面临最后期限,并且重构 MasterAccountMasterAccountID 会让我远远超过最后期限。

我怎样才能让它工作?

编辑:异常堆栈跟踪:

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:

编辑 2:当我使用 Moho 的建议时:

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

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

我的配置文件:

public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}

protected override void Seed(MyDbContext context)
{
}

最佳答案

您是否尝试过使用 InverseProperty 属性装饰集合属性?

[InverseProperty( "MasterAccount" )]
public virtual List<SystemAccount> AllSubAccounts { get; set; }

[InverseProperty( "SecondMasterAccount" )]
public virtual List<SystemAccount> SecondarySubAccounts { get; set; }

这是一个适合我的演示:

public class HierarchicalEntity
{
public int Id { get; set; }
public string Description { get; set; }

[ForeignKey( "PrimaryParent" )]
public int? PrimaryParentId { get; set; }

[ForeignKey( "SecondaryParent" )]
public int? SecondaryParentId { get; set; }
public virtual HierarchicalEntity PrimaryParent { get; set; }

public virtual HierarchicalEntity SecondaryParent { get; set;}

[InverseProperty( "PrimaryParent" )]
public ICollection<HierarchicalEntity> ChildrenViaPrimaryParent { get; set; }

[InverseProperty( "SecondaryParent" )]
public ICollection<HierarchicalEntity> ChildrenViaSecondaryParent { get; set; }
}

multiple self-references

关于c# - 同一张表中的两个一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com