gpt4 book ai didi

c# - ASP.Net 与标识模型的一对一或零关系

转载 作者:太空宇宙 更新时间:2023-11-03 15:07:08 24 4
gpt4 key购买 nike

我正在尝试建立从类模型 Figure 到 ApplicationUser 的关系。 ApplicationUser 可能与 Figure 没有任何关系,或者与 Figure 完全没有关系。 Figure 总是与 ApplicationUser 有关系。

我正在尝试在我的代码中这样做:

人物模型

public class Figure
{
[Key]
public string FigureId { get; set; }

public string Description { get; set; }

public string UserId { get; set; }

[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
}

应用用户模型

public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }

[Required]
public string LastName { get; set; }

[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime BirthDate { get; set; }

public string Occupation { get; set; }

public string Location { get; set; }

public virtual Figure Figure { get; set; }

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}

然后我在包管理器控制台中运行 add-migration Figure 时得到了这个:

System.InvalidOperationException: Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'Lacivi.Models.ApplicationUser'.
at System.Data.Entity.Internal.DbSetDiscoveryService.RegisterSets(DbModelBuilder modelBuilder)
at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
at System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Scaffold(String migrationName, String language, String rootNamespace, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.Execute(String name, Boolean force, Boolean ignoreChanges)
at System.Data.Entity.Migrations.AddMigrationCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'Lacivi.Models.ApplicationUser'.

最后一行是红色的。我想知道这一定是问题所在,但我不明白。

最佳答案

您的 DbContext 类是什么样的?如果您继承自 IdentityDbContext<Lacivi.Models.ApplicationUser>并添加了这个属性

public DbSet<Lacivi.Models.ApplicationUser> ApplicationUsers { get; set; }

这很可能是问题所在,因为 IdentityDbContext<T>类已经包含一个名为 Users 的相同类型的属性

关于c# - ASP.Net 与标识模型的一对一或零关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42873085/

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