开发者

Entity Framework Code First Configure Schema Per Entity

In LINQ to SQL there is a very simple convention to tell the mapper what database schema to use - [Table(Name = "SchemaName.TableName")].

Is there something similar in EF CF 4.1? I would rather not use annotations in the entity classes to keep them as pure as possible. For the sake of keeping my project moving I'll make the sacrifice though.

This question is close, but not exactly what I need. I开发者_Python百科t may even be outdated as it looks like it's referring to classes that have been renamed or have had breaking changes since the recent EF 4.1 release - Entity Framework 4: Code First - Creating db in another schema? MapSingleType?

EDIT: I should also mention that my application has three schemas at the moment, so I can't necessarily use a solution that changes only the default schema from "dbo" to "otherschema".


The linked answer is old and it doesn't work because custom conventions were removed from final version. If you want to change name of schema you can use fluent api:

public class Context : DbContext
{
    public DbSet<YourType> YourTypeSet { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<YourType>().ToTable("TableName", "SchemaName");
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜