实体框架4:代码优先 - 在另一个架构中创建数据库? MapSingleType? [英] Entity Framework 4: Code First - Creating db in another schema? MapSingleType?

查看:85
本文介绍了实体框架4:代码优先 - 在另一个架构中创建数据库? MapSingleType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,我使用2个不同的模式。模式就像命名空间(如果我错了,纠正我)。这样我有1个分组,目前有2个模式...所以1模式中的表可以命名为另一个模式中的表相同,因为它们在不同的模式中。



如何获取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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆