实体框架4:代码优先 - 在另一个架构中创建数据库? MapSingleType? [英] Entity Framework 4: Code First - Creating db in another schema? MapSingleType?
问题描述
如何获取EF代码首先与不同的模式进行交互,而不是默认模式?是否与MapSingleType有关,并覆盖方法或可以我做一些其他的事情?
非常感谢。
您可以执行以下约定:
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(
类型memberInfo,Func< EntityTypeConfiguration>配置)
{
EntityTypeConfiguration cfg = );
string tableName = cfg.EntitySetName;
if(String.IsNullOrEmpty(tableName))
tableName = memberInfo.Name;
cfg.ToTable(tableName,this.defaultSchema);
}
}
用法:
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));
}
Arthur Vickers有一些附注这里关于TPT继承和多对多关系。
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.
How do i get EF Code first to talk to a different schema and not the default schema?
Is it somethign to do with MapSingleType and overriding a method or can i do something else?
ANy help really appreciated.
You can implement the following convention:
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"));
}
There is a couple of side notes by Arthur Vickers here concerning TPT inheritance and many-to-many relations.
这篇关于实体框架4:代码优先 - 在另一个架构中创建数据库? MapSingleType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!