数据库迁移在EF Core中生成错误的表设计(使用AspNetUsers的多对多)

时间:2016-11-02 23:28:18

标签: asp.net-core entity-framework-core

我想在Project表(AspNetUser,MVC Core项目模板)中添加AspNetUsers个实体列表:

public class Project
{
    public int ProjectId { get; set; }
    public IList<ApplicationUser> User { get; set; }
}

Add-Migration在我的ProjectId表格中添加AspNetUsers列。到目前为止还可以。

Project添加ApplicationUser列表会导致错误。

代码:

public class ApplicationUser : IdentityUser
{
    public List<Project> Project { get; set; }
}

错误:

System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'Project.User' of type 'IList<ApplicationUser>'. Either manually configure the relationship, or ignore this property from the model.    at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)
    at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder) 
    at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator) 
    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) 
    at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() 
    at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.ScopedCallSite.Invoke(ServiceProvider provider) 
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
    at Microsoft.Extensions.DependencyInjection.ServiceLookup.ConstructorCallSite.Invoke(ServiceProvider provider) 
    at Microsoft.Extensions.DependencyInjection.ServiceProvider.TransientCallSite.Invoke(ServiceProvider provider)
    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) 
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
    at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) 
   at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action`1 reporter)
    at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0() 
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
    at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
 Unable to determine the relationship represented by navigation property 'Project.User' of type 'IList<ApplicationUser>'. Either manually configure the relationship, or ignore this property from the model. 

我缺少什么?!我必须使用Fluent API吗?!我想在AspNetUsers和我的Project-Entity之间建立多对多的关系。

1 个答案:

答案 0 :(得分:3)

多对多关系需要介于两者之间的实体。您需要一个引用User和Project

的实体
public class ProjectUser
{
    public int ProjectId { get; set; }
    public Project Project { get; set; }

    public int UserId { get; set; }
    public ApplicationUser User { get; set; }
}

在您现有的课程中

public class Project 
{
    public int ProjectId { get; set; }
    public List<ProjectUser> ProjectUsers { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public List<ProjectUser> ProjectUsers { get; set; }
}

在你的DbContext

public class MyContext : DbContext
{
    public DbSet<Project> Projects { get; set; }
    public DbSet<ApplicationUser> Users { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ProjectUser>()
            .HasKey(x => new { x.ProjectId, x.UserId });

        modelBuilder.Entity<ProjectUser>()
            .HasOne(p => p.Project)
            .WithMany(pu => pu.ProjectUsers)
            .HasForeignKey(p => p.ProjectId);

        modelBuilder.Entity<ProjectUser>()
            .HasOne(u => u.User)
            .WithMany(pu => pu.ProjectUsers)
            .HasForeignKey(u => u.UserId);
    }
}

可在https://docs.efproject.net/en/latest/modeling/relationships.html#

找到更多信息