0

I have an Entity Model (EDMX) file and EF 4.3.1. I am trying to make a run-time modification to the EDMX (change the store:Schema of the tables/entitySets used in generating the query). I am using code based on the EF Model Adapter project by B. Haynes.

It appears that I can make the changes to the XML just fine using the schema model adapter, and load it into a metadata workspace and then pass it to the connection. However, when the query is generated by the DbContext/EF framework code, it uses the old value for the schema.

  1. Create a new MyEntities
  2. Load the EDMX medata data manually
  3. Replace the "store:Schema" value with the new desired value
  4. Create the metadata workspace from the modified XML
  5. Return a new EntityConnection using that modified workspace
  6. Query the data (from x in db.Table select x)

This is the basics of what is going on. We create our dbContext by creating a new EntityConnection based on the modified workspace and the connection. There is also some provider wrapping and such going on, for logging, etc. Sorry if that's confusing.

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,...)
}

Any idea where/why it is still getting the old Schema value for the query? I think it worked right with EF 4.0,

4

1 に答える 1

0

問題は<DefiningQuery>、エンティティ セットの下の要素にあることが判明しました。

この要素には、エンティティの定義に使用されるベース クエリの定義が含まれます。おそらく何かが変更され、現在は速度の理由でそれを参照しています。そのクエリも変更する必要があり、スキーマの変更が有効になります。

<EntitySet Name="MYTABLE" store:Type="Tables" store:Schema="MYSCHEMA" ...>
<DefiningQuery>
    SELECT MYTABLE.COLUMN [...REPEAT..]
    FROM MYSCHEMA.MYTABLE AS MYTABLE
</definingQuery>

そのため、これらの両方の場所を変更"MYSCHEMA"すると修正されます。要素だけstore:Schemaでは不十分です。

于 2013-03-12T09:56:02.360 に答える