2017-03-18 67 views
2

我想从类模型图到ApplicationUser之间建立关系。应用程序用户可能与图无关,或与图完全单一的关系。图总是和ApplicationUser有关系。ASP.Net与标识模型的一对一或零关系

我试图做到这一点在我的代码:在包管理器运行add-migration Figure

图模型

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模式

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

然后我得到这个控制台:

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

最后一行显示为红色。我想知道这肯定是问题,但我不明白。

回答

0

你的DbContext类是什么样的?如果从IdentityDbContext<Lacivi.Models.ApplicationUser>继承并添加此属性

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

这是最有可能的问题,因为IdentityDbContext<T>类已经包含相同类型的属性称为用户

+0

我没有,除了那些财产,'名字添加任何东西''LastName'''BirthDate'等 –

+0

我在说你的dbcobtext类。您没有粘贴这部分代码。你可以在这里添加它吗? – Tacud

+0

公共类ApplicationDbContext:IdentityDbContext { 公共ApplicationDbContext() :基部( “DefaultConnection”,throwIfV1Schema:假) { } 公共静态ApplicationDbContext创建() { 返回新ApplicationDbContext(); } public System.Data.Entity.DbSet Figures {get;组; } public System.Data.Entity.DbSet ApplicationUsers {get;组; } } –

相关问题