以下是我的表结构和上下文:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace AspnetIdentitySample.Models
{
public class Employee : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<MyTask> MyTasks { get; set; }
public virtual ICollection<Customer> Customers { get; set; }
}
public class MyTask
{
public int MyTaskId { get; set; }
public string Description { get; set; }
public DateTime OldDate { get; set; }
public DateTime NewDate { get; set; }
public bool IsDone { get; set; }
public virtual Employee User { get; set; }
}
public class Customer
{
public int CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual Employee Employee { get; set; }
}
public class MyDbContext : IdentityDbContext<Employee>
{
public MyDbContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Change the admin of the table to be Users instead of AspNetUsers
modelBuilder.Entity<IdentityUser>()
.ToTable("Employee");
modelBuilder.Entity<Employee>()
.ToTable("Employee");
}
public DbSet<MyTask> MyTasks { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Employee> Employees { get; set; }
}
}
当我尝试使用Entity Framework创建CRUD控制器时,它显示以下错误消息。任何帮助,将不胜感激。 :)
答案 0 :(得分:4)
OP添加了更多细节和代码。原始答案已过时且错误
/编辑:
您正试图将2个模型绑定到同一个表:
modelBuilder.Entity<IdentityUser>()
.ToTable("Employee");
modelBuilder.Entity<Employee>()
.ToTable("Employee");
另外,您尝试添加已经存在于datsabase中的public DbSet<Employee> Employees { get; set; }
作为上下文类定义所定义的标识表。
我没有尝试重命名aspnetusers表,但我尝试删除以下行:
modelBuilder.Entity<IdentityUser>()
.ToTable("Employee");
public DbSet<Employee> Employees { get; set; }
这应该可以解决问题。