实体框架4:代码第一 - 在另一个模式创建分贝? MapSingleType? [英] Entity Framework 4: Code First - Creating db in another schema? MapSingleType?
问题描述
我有一个数据库,并使用2种不同的模式我。架构就像命名空间(纠正我,如果我错了)。这样,我有1 db和目前2模式......所以在1模式中的表可以被命名为相同的其他架构,因为他们在不同的模式中的表。
I have a database and i using 2 different schemas. Schemas are like namespaces (correct me if i am wrong). This way i have 1 db and currently 2 schemas... so the tables in 1 schema can be named the same as the tables in the other schema because they are in separate schemas.
我如何获得EF代码第一次跟一个不同的模式,而不是默认模式?
How do i get EF Code first to talk to a different schema and not the default schema?
是不是somethign做MapSingleType和覆盖方法还是可以的我做点别的?
Is it somethign to do with MapSingleType and overriding a method or can i do something else?
任何帮助非常感激。
推荐答案
您可以实现以下约定:
public class DefaultSchemaConvention :
IConfigurationConvention<Type, EntityTypeConfiguration>
{
string defaultSchema;
public DefaultSchemaConvention(string defaultSchema)
{
if (String.IsNullOrWhiteSpace(defaultSchema))
throw new ArgumentException("defaultSchema");
this.defaultSchema = defaultSchema;
}
void IConfigurationConvention<Type, EntityTypeConfiguration>.Apply(
Type memberInfo, Func<EntityTypeConfiguration> configuration)
{
EntityTypeConfiguration cfg = configuration();
string tableName = cfg.EntitySetName;
if (String.IsNullOrEmpty(tableName))
tableName = memberInfo.Name;
cfg.ToTable(tableName, this.defaultSchema);
}
}
用法:
Usage:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.Edm.Db.ColumnTypeCasingConvention>();
modelBuilder.Conventions.Add(new DefaultSchemaConvention("TEST"));
}
有一个由阿瑟·维克斯的>有关TPT的继承和许多一对多的关系。
There is a couple of side notes by Arthur Vickers here concerning TPT inheritance and many-to-many relations.
这篇关于实体框架4:代码第一 - 在另一个模式创建分贝? MapSingleType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!