无法在AspNetUsers和自定义表之间创建一对多关系

时间:2016-01-22 07:34:24

标签: asp.net asp.net-mvc-4 asp.net-identity

我正在尝试在AspnetUsers和Trip之间建立一对多的关系。 我的旅行课程如下:

public class Trip
{
    public int TripId { get; set; }
    public string TripName { get; set; }

    public string ApplicationUserId { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }
}

我在IdentityModel中添加了以下内容:

public virtual ICollection<Trip> Trips {get; set; }

现在,当我尝试运行此数据库或更新数据库时,我收到以下错误:

System.InvalidOperationException: Multiple object sets per type are not   supported. The object sets 'ApplicationUsers' and 'Users' can both contain   instances of type 'IdentityRelationship.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.ScaffoldInitialCreate(String language, String rootNamespace)
at System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

不支持每种类型的多个对象集。对象设置&#39; ApplicationUsers&#39;和&#39;用户&#39;都可以包含类型为&#39; IdentityRelationship.Models.ApplicationUser&#39;的实例。

有谁能告诉我发生了什么以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

在我认为的Trip类中,应该包含IdentityModel的外键虚拟属性(导航属性)。

例如:

public class Trip{

public int TripId { get; set; }
public string TripName { get; set; }
public string ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }

public int IdentityModelId{ get; set; }

[ForeignKey("IdentityModelId")]
public virtual IdentityModel IdentityModel{get; set; }
}

此外,应该可以将集合填充到IdentityModel类的Trips属性中。