2017-03-18 47 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;組; } } –