假设是这种情况,我想为每个类(包括抽象)创建一个单独的表。
这是DBContext:
type Direction = { [name: string]: number };
const Direction = { ... } as Direction;
实体内容
PersonConfiguration.cs
public class AIOmedicareContext : DbContext
{
public AIOmedicareContext() : base("name=AIOmedicareConnectionString")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Role>()
.ToTable("Roles")
.HasKey(p => p.PrimaryKey)
.Property(p => p.PrimaryKey)
.HasColumnName("RoleID")
.HasColumnOrder(1);
// Moved all Person related configurations to PersonConfiguration class.
modelBuilder.Configurations.Add(new PersonConfiguration());
// Moved all User related configurations to UserConfiguration class.
modelBuilder.Configurations.Add(new UserConfiguration());
// Moved all Employee related configurations to EmployeeConfiguration class.
modelBuilder.Configurations.Add(new EmployeeConfiguration());
base.OnModelCreating(modelBuilder);
}
}
EmployeeConfiguration.cs
public class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
// Database table name
this.ToTable("Person")
.HasKey<Guid>(p => p.PrimaryKey);
// every person have atmost one account.
this.HasOptional<Account>(s => s.Account);
// PrimaryKey property mapped to PersonID column in database.
this.Property(p => p.PrimaryKey)
.HasColumnName("PersonID")
.HasColumnOrder(1);
// a person can have many contacts.
this.HasMany<Contact>(c => c.Contacts)
.WithRequired(p => p.Person)
.HasForeignKey(c => c.PersonID);
// a person can have many addresses.
this.HasMany<Address>(p => p.Addresses)
.WithRequired(a => a.Person)
.HasForeignKey(a => a.PersonID);
}
}
UserConfiguration.cs
class EmployeeConfiguration : EntityTypeConfiguration<Employee>
{
public EmployeeConfiguration()
{
this.ToTable("Employees")
.HasKey(p => p.PrimaryKey)
.Property(e => e.PrimaryKey)
.HasColumnName("EmployeeID")
.HasColumnOrder(1);
this.HasMany<Role>(u => u.Roles)
.WithMany(e => e.Employees)
.Map(re => {
re.MapLeftKey("EmployeeRefID");
re.MapRightKey("RoleRefID");
re.ToTable("EmployeeRole");
});
}
}
我正在尝试通过控制台应用程序创建用户的对象,但它会抛出以下异常。
Program.cs的
class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
this.ToTable("Users")
.HasKey(p => p.PrimaryKey)
.Property(u => u.PrimaryKey)
.HasColumnName("UserID")
.HasColumnOrder(1);
this.HasMany(u => u.UserLogs)
.WithRequired(ul => ul.User)
.HasForeignKey(u => u.UserID);
this.HasMany(u => u.Permissions)
.WithOptional(p => p.User)
.HasForeignKey(p => p.UserID);
}
}
这是错误。
从ObjectStateEntry中检索值时出错。有关详细信息,请参阅内部异常。
内在的例外是:
(23,10):错误3032:从第23,34行开始映射片段时出现问题:实体类型AIOmedicare.DAL.EF.Employee,AIOmedicare.DAL.EF.Customer,AIOmedicare.DAL.EF.User正在映射表Person中的相同行。映射条件可用于区分这些类型映射到的行。
迁移已成功应用,并已通过SQL Server Management Studio验证表是否存在。
我错过了什么?