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

时间:2022-08-31 08:45:48

I am trying to build one to many relationship between AspnetUsers and Trip. My Trip class is as given below:

我想在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; }
}

I have added following in IdentityModel:

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

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

Now when I try to run this or update-database I get the following error:

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

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)

Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'IdentityRelationship.Models.ApplicationUser'.

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

Can anyone please tell me what's going on and how to solve this?

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

1 个解决方案

#1


0  

In my opinion class called Trip, should contains IdentityModel's foreign key virtual property (navigation property).

在我看来称为Trip的类中,应该包含IdentityModel的外键虚拟属性(navigation property)。

Fore example:

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

And afther that it should be possible to populate the collection into Trips property at IdentityModel class.

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

#1


0  

In my opinion class called Trip, should contains IdentityModel's foreign key virtual property (navigation property).

在我看来称为Trip的类中,应该包含IdentityModel的外键虚拟属性(navigation property)。

Fore example:

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

And afther that it should be possible to populate the collection into Trips property at IdentityModel class.

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