开发者

EF 4 Code First: How can I modify naming conventions for generated junction tables?

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 lower开发者_运维知识库case "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.


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:

modelBuilder.Entity<Offer>()
    .HasMany(o => o.Products)
    .WithMany(p => p.Offers)
    .Map(m => m.ToTable("tProduct_Offer"));

The code snippet is RC version. CTP5 used one more step with IsIndependent() but it was removed in RC.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜