【问题标题】:"Cannot use table in schema '' for entity since it is being used for another entity" error“无法将模式中的表用于实体,因为它正在用于另一个实体”错误
【发布时间】:2019-02-03 17:38:03
【问题描述】:

我们正在尝试将ASPNetUsers 表映射到我们的自定义表用户。下面是类及其关系。

public partial class Users     
{  
    public int Id { get; set; }       
    public string FirstName { get; set; }       
    public string MiddleName { get; set; }        
    public string LastName { get; set; }        
    public string UserInfoId { get; set; }    
    public virtual AspNetUsers UserInfo { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Users Users { get; set; }
}

当我们使用这些模型实现身份时,我们会收到以下错误:

“无法将架构 '' 中的表 'AspNetUsers' 用于实体 'AspNetUsers',因为它正用于另一个实体。”

我们没有对ASPNetUsers 表进行任何更改;就像我们通过迁移一样。

我们如何解决这个问题?

【问题讨论】:

  • link的可能重复

标签: .net


【解决方案1】:

在您的ApplicationDbContext 中,您必须自定义AspNetUsers 表:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    new AspNetUsersConfiguration(builder.Entity<AspNetUsers>());
}

在你的配置文件中:

public class AspNetUsersConfiguration
{
    public AspNetUsersConfiguration(EntityTypeBuilder<AspNetUsers> entity)
    {
     entity.ToTable("AspNetUsers", "dbo");
     entity.HasKey(e => e.Id);
    }
}

关键答案是定义一个架构dbo 到表AspNetUsers

【讨论】:

    猜你喜欢
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多