Entity framework 生成SQL时忽略修改的实体工作区

Entity framework 生成SQL时忽略修改的实体工作区,entity-framework,entity-framework-4,dbcontext,Entity Framework,Entity Framework 4,Dbcontext,我有一个实体模型(EDMX)文件和EF 4.3.1。我正在尝试对EDMX进行运行时修改(更改生成查询时使用的表/实体集的store:Schema)。我使用的代码基于B.Haynes的项目 似乎我可以使用模式模型适配器对XML进行更改,并将其加载到元数据工作区,然后将其传递给连接。但是,当查询由DbContext/EF框架代码生成时,它将使用模式的旧值 创建新的MyEntities 手动加载EDMX medata数据 用新的所需值替换“store:Schema”值 从修改后的XML创建元数据工作区

我有一个实体模型(EDMX)文件和EF 4.3.1。我正在尝试对EDMX进行运行时修改(更改生成查询时使用的表/实体集的
store:Schema
)。我使用的代码基于B.Haynes的项目

似乎我可以使用模式模型适配器对XML进行更改,并将其加载到元数据工作区,然后将其传递给连接。但是,当查询由DbContext/EF框架代码生成时,它将使用模式的旧值

  • 创建新的MyEntities
  • 手动加载EDMX medata数据
  • 用新的所需值替换“store:Schema”值
  • 从修改后的XML创建元数据工作区
  • 使用修改后的工作区返回新的EntityConnection
  • 查询数据(
    来自数据库中的x。表选择x
  • 这是正在发生的事情的基础。我们通过基于修改后的工作区和连接创建新的EntityConnection来创建dbContext。还有一些提供程序包装等正在进行,用于日志记录等。如果这让人困惑,很抱歉

    public MyEntities(): base( this.Create("name=MyEntitiesConnStr"), true)
    {
    }
    
    public static DbConnection Create(string connectionString)
    {
        var ecsb = ConnectionHelper.ResolveConnectionStringDetails(connectionString);
        var workspace = GetModifiedEntityWorkspace(ecsb);
        var storeConnection = DbProviderFactories.GetFactory(ecsb.Provider).CreateConnection();
        Debug.Assert(storeConnection != null, "storeConnection != null");
        storeConnection.ConnectionString = ecsb.ProviderConnectionString;
        var wrappedConnection = MyWrappedConnetion.WrapConnection(storeConnection);
        _log.Debug("Creating new entity connection");
        var newEntityConnection = new EntityConnection(workspace, wrappedConnection);
        WireEvents(wrappedConnection);
        return newEntityConnection;
    }
    
    private static MetadataWorkspace GetModifiedEntityWorkspace(EntityConnectionStringBuilder ecsb)
    {
        // instantiate manager class
        // read all XML items from the embedded resources
        // change the store:schema to the real one for this environment
        // <EntitySet Name="..." store:Type="Tables" store:Schema="SCM" store:Name="TBLX">
        // create new MetadataWorksspace(ssdl,cdl,...)
    }
    
    public myenties():base(this.Create(“name=myentiesconnstr”),true)
    {
    }
    公共静态数据库连接创建(字符串连接字符串)
    {
    var ecsb=ConnectionHelper.ResolveConnectionString详细信息(connectionString);
    var workspace=GetModifiedEntityWorkspace(ecsb);
    var storeConnection=dbProviderFactorys.GetFactory(ecsb.Provider.CreateConnection();
    Assert(storeConnection!=null,“storeConnection!=null”);
    storeConnection.ConnectionString=ecsb.ProviderConnectionString;
    var wrappedConnection=mywrappedconnection.WrapConnection(storeConnection);
    _调试(“创建新实体连接”);
    var newEntityConnection=新EntityConnection(工作区,wrappedConnection);
    WireEvents(wrappedConnection);
    返回newEntityConnection;
    }
    私有静态MetadataWorkspace GetModifiedEntityWorkspace(EntityConnectionStringBuilder ecsb)
    {
    //实例化管理器类
    //从嵌入的资源中读取所有XML项
    //为此环境将store:schema更改为实际模式
    // 
    //创建新的MetadataWorkspace(ssdl、cdl等)
    }
    

    是否知道它在何处/为什么仍然获取查询的旧
    模式
    值?我认为它在EF4.0中工作正常,

    结果是实体集下的
    元素出现了问题

    此元素包含用于定义实体的基本查询的定义。也许有些事情改变了,现在他们提到这一点是因为速度的原因。还需要修改该查询,然后模式更改将生效

    <EntitySet Name="MYTABLE" store:Type="Tables" store:Schema="MYSCHEMA" ...>
    <DefiningQuery>
        SELECT MYTABLE.COLUMN [...REPEAT..]
        FROM MYSCHEMA.MYTABLE AS MYTABLE
    </definingQuery>
    
    
    选择MYTABLE.COLUMN[…重复..]
    从MYSCHEMA.MYTABLE作为MYTABLE
    
    因此,在这两个位置更改
    “MYSCHEMA”
    可以修复它。仅使用
    store:Schema
    元素是不够的