【问题标题】:Can't create one to many relationship between AspNetUsers and Custom Table无法在 AspNetUsers 和自定义表之间创建一对多关系
【发布时间】:2016-01-22 07:34:24
【问题描述】:

我正在尝试在 AspnetUsers 和 Trip 之间建立一对多的关系。 我的 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)

不支持每种类型的多个对象集。对象集“ApplicationUsers”和“Users”都可以包含“IdentityRelationship.Models.ApplicationUser”类型的实例。

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

【问题讨论】:

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


    【解决方案1】:

    在我看来名为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 属性中。

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 2021-04-19
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多