【发布时间】:2013-12-26 17:09:13
【问题描述】:
我对自定义用户有疑问。 我有一个名为 Profiles 的表,用于我的 users 表(它不允许我使用 Users,但这是一个不同的问题!)我创建了这样的 dbcontext:
public partial class SkipstoneContext : IdentityDbContext<Profile>
{
static SkipstoneContext()
{
Database.SetInitializer<SkipstoneContext>(null); // Existing data, do nothing
}
public SkipstoneContext()
: base("DefaultConnection")
{
}
// ...
public DbSet<Company> Companies { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(model => model.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(model => model.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(model => new { model.RoleId, model.UserId });
modelBuilder.Entity<UserSecret>().HasKey<string>(model => model.UserName);
}
}
如您所见,我不必为 Profile 创建一个 DbSet,因为它是在继承的 IdentityDbContext 类中设置的 p>
我的个人资料类如下所示:
public partial class Profile : IdentityUser
{
public Profile()
{
// ...
}
public string CompanyId { get; set; }
public string Title { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string Email { get; set; }
// ...
public virtual Company Company { get; set; }
// ...
}
当我运行我的项目时,我收到一条错误提示
对象名称“dbo.IdentityUsers”无效。
如果我将表重命名为 IdentityUsers 并再次运行该项目,我会收到一条错误提示
无效的对象名称“dbo.Profiles”。
这就是令人困惑的地方。看来 EF 正在为用户寻找 2 个表,而不仅仅是一个。
谁能给我解释一下为什么?
更新 1
因此,为了解决这个问题,我从 SkipstoneContext 中删除了继承的 IdentityDbContent,而改为从 DbContext 继承。 新的上下文类如下所示:
public partial class SkipstoneContext : DbContext // : IdentityDbContext<Profile>
{
static SkipstoneContext()
{
Database.SetInitializer<SkipstoneContext>(null); // Exsting database, do nothing
}
public SkipstoneContext()
: base("DefaultConnection")
{
}
// ...
public DbSet<Company> Companies { get; set; }
public DbSet<Profile> Profiles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// ...
modelBuilder.Configurations.Add(new CompanyMap());
modelBuilder.Configurations.Add(new ProfileMap());
/// ...
//modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(model => model.UserId);
//modelBuilder.Entity<IdentityRole>().HasKey<string>(model => model.Id);
//modelBuilder.Entity<IdentityUserRole>().HasKey(model => new { model.RoleId, model.UserId });
//modelBuilder.Entity<UserSecret>().HasKey<string>(model => model.UserName);
}
}
我还注释掉了我对 IdentityUserLogin、IdentityRole、IdentityUserRole 和 UserSecret 的绑定,这就是开始工作了。
看看IdentityUserLogin:
public class IdentityUserLogin
{
public IdentityUserLogin();
public virtual string LoginProvider { get; set; }
public virtual string ProviderKey { get; set; }
public virtual IdentityUser User { get; set; }
public virtual string UserId { get; set; }
}
它有一个 IdentityUser 属性,我认为这是导致我的问题的原因。我知道 Profile 继承了 IdentityUser 但由于某种原因它似乎认为它在另一个表中。
因此,我的下一个任务是创建自定义 UserLogin 并查看是否可以让 EF 使用 IdentityUserLogin
的插入【问题讨论】:
-
您有
DbSet对应的Profile和IdentityUser吗? -
不,这就是我要说的。 IdentityDbContext 应该处理我的用户表(我假设它会很乐意使用 Profiles,因为我的 User 类称为 Profile)但它似乎由于某种原因而倒下
标签: c# sql entity-framework asp.net-mvc-5