Entity framework 如何获得实体';使用代码优先(实体框架)和DbContext的架构名称?

Entity framework 如何获得实体';使用代码优先(实体框架)和DbContext的架构名称?,entity-framework,schema,code-first,dbcontext,Entity Framework,Schema,Code First,Dbcontext,我们的数据库是以这样一种方式设计的,其中有用于生产的各种模式和用于测试的各种等效模式。例如,许多表位于MyProduction模式中,而相同的表位于MyTest模式中 我想做的是确定表使用的是哪个模式,这样我就知道要将它更改为哪个模式。因此,默认情况下,所有内容都在生产模式下。在DbContext的OnModelCreating事件中,如果需要指向test(由一些真/假配置确定),则需要确定正在使用的生产模式,然后将其指向它的测试等价物 我已经知道如何设置模式,但找不到如何获得它。知道如何确定表

我们的数据库是以这样一种方式设计的,其中有用于生产的各种模式和用于测试的各种等效模式。例如,许多表位于MyProduction模式中,而相同的表位于MyTest模式中

我想做的是确定表使用的是哪个模式,这样我就知道要将它更改为哪个模式。因此,默认情况下,所有内容都在生产模式下。在DbContext的OnModelCreating事件中,如果需要指向test(由一些真/假配置确定),则需要确定正在使用的生产模式,然后将其指向它的测试等价物

我已经知道如何设置模式,但找不到如何获得它。知道如何确定表使用的模式吗


谢谢。

实体框架架构是System.ComponentModel.DataAnnotations.TableAttribute对象。这里有一些方法可以用来获取实体的模式名和表名。干杯

    private string GetTableName(Type type)
    {
        var tableAttribute = type.GetCustomAttributes(false).OfType<System.ComponentModel.DataAnnotations.TableAttribute>().FirstOrDefault();
        if (tableAttribute != null && !string.IsNullOrEmpty(tableAttribute.Name))
        {
            return tableAttribute.Name;
        }
        else
        {
            return string.Empty;
        }
    }

    private string GetTableSchema(Type type)
    {
        var tableAttribute = type.GetCustomAttributes(false).OfType<System.ComponentModel.DataAnnotations.TableAttribute>().FirstOrDefault();
        if (tableAttribute != null && !string.IsNullOrEmpty(tableAttribute.Schema))
        {
            return tableAttribute.Schema;
        }
        else
        {
            return string.Empty;
        }
    }
私有字符串GetTableName(类型)
{
var tableAttribute=type.GetCustomAttributes(false).OfType().FirstOrDefault();
if(tableAttribute!=null&&!string.IsNullOrEmpty(tableAttribute.Name))
{
返回tableAttribute.Name;
}
其他的
{
返回字符串。空;
}
}
私有字符串GetTableSchema(类型)
{
var tableAttribute=type.GetCustomAttributes(false).OfType().FirstOrDefault();
if(tableAttribute!=null&&!string.IsNullOrEmpty(tableAttribute.Schema))
{
返回tableAttribute.Schema;
}
其他的
{
返回字符串。空;
}
}

根据您的本地设置进行修改后,请尝试以下代码:

var context = new YouDbContext("ConnectionName");
var adapter = (IObjectContextAdapter)context;
var objectContext = adapter.ObjectContext;
EntitySetBase schema = null;
if (objectContext.MetadataWorkspace != null)
{
    schema = objectContext.MetadataWorkspace
        .GetItems<EntityContainer>(DataSpace.SSpace).First()
        .BaseEntitySets
        .First(meta => meta.ElementType.Name == "ClassNameUnderYourDbContext");
}
//See the properties of schema in debug mode to understand details
var context=newyoudbcontext(“ConnectionName”);
var adapter=(IObjectContextAdapter)上下文;
var objectContext=adapter.objectContext;
EntitySetBase模式=null;
if(objectContext.MetadataWorkspace!=null)
{
schema=objectContext.MetadataWorkspace
.GetItems(DataSpace.SSpace).First()
.BaseEntitySets
.First(meta=>meta.ElementType.Name==“YourDbContext下的ClassName”);
}
//查看调试模式下架构的属性以了解详细信息

您不需要获取它-您负责映射,因此您可以定义实体映射到哪个架构和表。我确实需要它。我必须使用至少6个单独的模式。三个模式用于测试,三个用于生产。每个都有100张桌子。编写会更加高效:如果这个匿名表正在使用这个生产模式,那么将其更改为MyTestSchema(如果需要),而不是管理三个单独的100个表列表,每个表都有可能的配置。无论如何,我的类和上下文都是由CodeFirstTemplate生成器从EDM生成的,因此模式应该可以获取。我的问题不是,我是否需要这样做,我的问题是如何做。谢谢,这并不重要,但在切换到实体框架之前,我可以使用NHibernate这样做。我更喜欢实体框架,但我仍然需要实现这个功能,这就是为什么我希望在OnModelCreating事件中实现它。比方说,我去掉了EDMX,这样我就可以使用OnModuleCreating,我花时间在我的表类上放置表注释,并在其中放入一个模式名:[table(“Authors”,schema=“MyProdSchema”)]。是否有方法从该事件中的实体获取注释信息?如果要获取所有架构,请从查询中删除。第一(…)部分。请参阅
var context = new YouDbContext("ConnectionName");
var adapter = (IObjectContextAdapter)context;
var objectContext = adapter.ObjectContext;
EntitySetBase schema = null;
if (objectContext.MetadataWorkspace != null)
{
    schema = objectContext.MetadataWorkspace
        .GetItems<EntityContainer>(DataSpace.SSpace).First()
        .BaseEntitySets
        .First(meta => meta.ElementType.Name == "ClassNameUnderYourDbContext");
}
//See the properties of schema in debug mode to understand details