简体   繁体   中英

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

I am trying to make a relationship from class model Figure to ApplicationUser. ApplicationUser probably have no relation to Figure or exactly single relation to Figure. Figure always have a relationship to 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

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 :

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? If you inherited from IdentityDbContext<Lacivi.Models.ApplicationUser> and added this property

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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