Mar*_*tin 18 c# schema entity-framework-4 ef4-code-only ef-code-first
我有一个数据库,我使用2种不同的模式.模式就像名称空间(如果我错了,请纠正我).这样我有1个db和当前2个模式...因此1个模式中的表可以与其他模式中的表命名相同,因为它们位于不同的模式中.
如何让EF Code首先与不同的模式进行通信而不是默认模式?
是否与MapSingleType和覆盖方法有关,还是我可以做其他事情?
任何帮助真的很感激.
Dev*_*art 10
您可以实现以下约定:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
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"));
}
Run Code Online (Sandbox Code Playgroud)
亚瑟维克斯在这里有一些关于TPT继承和多对多关系的附注.
我不知道CodeFirst模型,但"Table"的DataAnnotation包含一个模式选项.我的代码如下:
<Table("Product", Schema:="SalesLT")>
Public Class Product
End Class
Run Code Online (Sandbox Code Playgroud)
这有助于我处理备用架构
归档时间: |
|
查看次数: |
5556 次 |
最近记录: |