简体   繁体   English

ASP.Net与身份模型的一对一或零关系

[英]ASP.Net One-to-One or Zero Relationship with Identity Models

I am trying to make a relationship from class model Figure to ApplicationUser. 我正在尝试从类模型Figure到ApplicationUser建立关系。 ApplicationUser probably have no relation to Figure or exactly single relation to Figure. ApplicationUser可能与Figure无关,也可能与Figure无关。 Figure always have a relationship to ApplicationUser. 图始终与ApplicationUser有关系。

I am trying to do this in my code : 我正在尝试在我的代码中执行此操作:

Figure Models 人物模型

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

AplicationUser Models Aplication用户模型

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

Then I got this when running add-migration Figure in package manager console : 然后我在包管理器控制台中运行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'.

The last line is colored red. 最后一行显示为红色。 I am wondering that must be the problem, but I don't understand. 我想知道那一定是问题,但我不明白。

What does your DbContext class look like? 您的DbContext类是什么样的? If you inherited from IdentityDbContext<Lacivi.Models.ApplicationUser> and added this property 如果您从IdentityDbContext<Lacivi.Models.ApplicationUser>继承并添加了此属性

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

That's most likely the problem because the IdentityDbContext<T> class already contains a property of the same type called Users 这很可能是问题所在,因为IdentityDbContext<T>类已经包含一个名为Users的相同类型的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM