EF 4 Code First:如何修改生成的联结表的命名约定?
我正在与现有数据库集成,因此我希望由 EF4 Code First 代码生成的表遵守已存在的表命名约定。
现有约定是单数实体名称,以小写“t”为前缀(例如tProduct
),并使用下划线连接连接表(例如tProduct_tOffer< /代码>)。
我已经使用下面的代码通过所有“标准”表实现了这一点。在我的 DbContext
中,我有:
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
modelBuilder.Conventions.Add<TableNamePrefixConvention>();
base.OnModelCreating(modelBuilder);
}
并且以下类将“t”添加到表名称中:
public class TableNamePrefixConvention : IConfigurationConvention<Type, EntityTypeConfiguration> {
public void Apply(Type typeInfo, Func<EntityTypeConfiguration> configuration) {
configuration().ToTable("t" + typeInfo.Name);
}
}
但是,当生成 junction 表时,它不遵守这些约定。现在,我的表列表中有这样的内容:
tOffer
tProduct
ProductOffers
在本例中,ProductOffers
应命名为 tProduct_tOffer
。如何强制生成的联结表符合我的命名风格?
更新:正如 Ladislav 在下面正确猜测的那样,我正在使用 CTP5。
I'm integrating with an existing database, so I would like the tables generated by my EF4 Code First code to adhere to the table naming conventions which are already in place.
The existing convention is the singular entity name, prefixed with a lowercase "t" (e.g tProduct
), and joined with an underscore for junction tables (e.g tProduct_tOffer
).
I've achieved this with all the 'standard' tables using the code below. In my DbContext
I have:
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<PluralizingEntitySetNameConvention>();
modelBuilder.Conventions.Add<TableNamePrefixConvention>();
base.OnModelCreating(modelBuilder);
}
And the following class adds the "t" to table names:
public class TableNamePrefixConvention : IConfigurationConvention<Type, EntityTypeConfiguration> {
public void Apply(Type typeInfo, Func<EntityTypeConfiguration> configuration) {
configuration().ToTable("t" + typeInfo.Name);
}
}
However, when a junction table is generated, it doesn't adhere to these conventions. So I now have this kind of thing in the list of tables:
tOffer
tProduct
ProductOffers
In this case, ProductOffers
should be named tProduct_tOffer
. How can I force the generated junction table to conform to my naming style?
Update: as Ladislav correctly guessed below, I am using CTP5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中指出的,自定义约定在 RC 版本中不可用,并且在 RTW 中也不可用 (官方公告)。
您必须手动映射它:
代码片段是 RC 版本。 CTP5 在
IsIndependent()
中多使用了一个步骤,但在 RC 中被删除。As I pointed in the comment custom conventions are not available in RC version and will not be available in RTW as well (official announcement).
You must map it manually:
The code snippet is RC version. CTP5 used one more step with
IsIndependent()
but it was removed in RC.