使用 CodeFirst 自定义多对多映射
开始在我的 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());
}
但是当我使用此模型运行应用程序时,发生了错误 - CodeFirst 试图找到某个表“dbo.People”(?!),但它不存在(预期)。感谢您的好回答!
Start using CodeFirst in my MVC projects and have some trouble.
I have in-use database with some predefined schema.
There are some tables:
[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
and many-to-many connected table
[QualRef]
[SemID] [bigint] NOT NULL,
[RefID] [bigint] NOT NULL
where SemID referenced to Seminars.ID and RefID referenced to Persons.ID
I'm trying to fix-up my CodeFirst schema binding using configuration class:
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");
});
}
}
and register it with this code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Person>().ToTable("Persons");
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new PersonConfiguration());
}
But when I run app using this models error occured - CodeFirst trying to find some table "dbo.People" (?!) and it does not exist (expected). Thank for good answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像这样更改代码,
//...
Try changing the code like this,
//...