在我的MVC项目中开始使用CodeFirst并遇到一些麻烦。 我有一些预定义模式的使用中的数据库。 有一些表格:
[Persons]
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](30) NOT NULL,
[Birthday] [datetime] NOT NULL,
[Address] [nvarchar](30) NOT NULL,
[Zip] [nvarchar](5) NOT NULL,
[City] [nvarchar](30) NOT NULL,
[Tax] [money] NOT NULL,
[Memo] [varbinary](max) NULL
[Seminars]
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](20) NOT NULL
和多对多连接表
[QualRef]
[SemID] [bigint] NOT NULL,
[RefID] [bigint] NOT NULL
其中SemID引用了Seminars.ID,RefID引用了Persons.ID
我正在尝试使用配置类来修复我的CodeFirst模式绑定:
class PersonConfiguration : EntityTypeConfiguration<Person>
{
internal PersonConfiguration()
{
this.HasMany(i => i.Seminars)
.WithMany(c => c.Persons)
.Map(mc =>
{
mc.MapLeftKey("SemID");
mc.MapRightKey("RefID");
mc.ToTable("QualRef");
});
}
}
并使用以下代码注册:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().ToTable("Persons");
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new PersonConfiguration());
}
但是当我使用这个模型运行app时出现错误 - CodeFirst试图找到一些表“dbo.People”(?!)并且它不存在(预期)。谢谢你的答案!
答案 0 :(得分:1)
尝试更改此代码,
class PersonConfiguration : EntityTypeConfiguration<Person>
{
public PersonConfiguration()
{
ToTable("Persons");
this.HasMany(i => i.Seminars)
.WithMany(c => c.Persons)
.Map(mc =>
{
mc.MapLeftKey("SemID");
mc.MapRightKey("RefID");
mc.ToTable("QualRef");
});
}
}
// ...
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PersonConfiguration());
base.OnModelCreating(modelBuilder);
}