views:

959

answers:

6

In most asp.net applications you can change the database store by modifing the connectionstring at runtime. i.e I can change from using a test database to a production database by simply changing the value of the "database" field in the connectionstring

I'm trying to change the schema (but not necessarily the database itself) with entity framework but no luck.

The problem I'm seeing is the that the SSDL content in the edmx xml file is storing the schema for each entityset.

see below

<EntitySet 
    Name="task" 
    EntityType="hardModel.Store.task" 
    store:Type="Tables" 
    Schema="test"  />

Now I have changed the schema attribute value to "prod" from test and it works..

But this does not seem to be a good solution.

  1. I need to update evert entity set as well as stored procedures ( I have +50 tables )
  2. I can only do this an compile time?
  3. If I then try to later update the Entity model-entityies that already exist are being read due to EF not recognizing that the table already exists in the edm.

Any thoughts?

+1  A: 

The connection string for EF is in the config file. There is no need to change the SSDL file.

EDIT

Do you have the prod and test schema in the same database?

If Yes you can fix it by using a seperate database for prod and test. Using the same schema name in both databases.

If No you can fix it by Using the same schema name in both databases.

If you will absolutly have different schema names, create two EF models, one for test and one for prod, then select which on to use in code based on a value in your config file.

Shiraz Bhaiji
This does not work. This was what i "assumed" would work.. but Entity Framework is not that easy.
We do exactly this on all of our EF projects, it works for us. We move the compiled dll's from dev to test to acceptance test to staging to production, only changing the config files. Are you certain that you are updating the correct config file?
Shiraz Bhaiji
Yes I am update the correct config file. I have created a demo solution with just one table and connectionstring and I'm having the same issue. Just to clarify; changing the web.config does make the EF hit the "correct" database. It is the output SQL that has an issue since it contains the "schema" as part of the output sql. Example "Select * from schemaName.tablename"
thanks for the input Shiraz.. This is such a common senario I do not see why it is an issue. The technical benefit I'm seeing is that it allows EF to use 2 databases/schema to model stuff. But this really sucks. The solutions you called about about having multple databases sounds doable but hm.. install another instance of mysql to run another version of my application. It isnt going to scale and well is not a real solution. Imagine if all .net apps need to this. Have 2 sql server instances for each version of the application. :(
You can have many databases in the same instance of sql server. But I am not sure about mySQL.
Shiraz Bhaiji
So you really need to change the schema, not the db that you are connected to? I hate it when I get this answer, but it seems right: perhaps you should reconsider the design. The Schema is really a part of the identity of the table. So, what you're sort of trying to do is to say "I want to change the names of all of the tables that I'm querying at run time." While that's doable, it's not very practical. I agree with the other commenters about putting your test data in a different database.
JMarsch
One other comment about putting your test data in a separate database: This allows you to have a separate backup for your test vs. production data. You probably don't want to blow away your production data every time you want to reset your test data. I don't know much about mysql, but SQL server does allow multiple databases on a server.
JMarsch
A: 

The easiest way to solve the problem is to manualy remove all entries like 'Schema="SchemaName"' from the SSDL part of the model.
Everything works propely in this case.

Devart
Ok.. I have done this. The problem is after I have updated the model the schema attribute comes back. Is there a 'harder' solution that will not break when I try to update the model?
This is a known limitation of Microsoft Update Model from database wizard - it overwrites all manual changes made to the storage model (SSDL).Users of dotConnect for MySQL have possibility to use the Entity Developer tool (it allows design time EF models editing and does not have the mentioned problem). Sorry, but MySQL Connector/NET is not supported by Entity Developer.
Devart
FYI - I am using dotConnect for MySQL but not the Entity Developer tool
+1  A: 

When I create a new "ADO.NET Entity Data Model", there are two properties "Entity Container Name" and "Namespace" available for editing in design view.. Using the namespace.EntityContainerName, you can create a new instance specifying a connection string.

MyEntities e = new MyEntities("connstr");
e.MyTable.Count();

I'm not sure if this helps you or not, good luck!

Also, this is a good case for multiple layers (doesn't have to be projects, but could be).

Solution
* DataAccess - Entities here
* Service - Wraps access to DataAccess
* Consumer - Calls Service

In this scenario, the consumer calls service, passing in whatever factor determines which connection string is used. The service then instantiates an instance of data access passing in the appropriate connection string and executes the consumer's query.

Ben
+1  A: 

Update Upon reading your comments it's clear that you're wanting to change the referenced schema for each DB, not the database. I've edited the question to clarify this and to restore the sample EDMX you provided which was hidden in the original formatting.

I'll repeat my comment below here:

If the schemata are in the same DB, you can't switch these at runtime (except with EF 4 code-only). This is because two identically-named and structured tables in two different schemata are considered entirely different tables.

I also agree with JMarsch above: I'd reconsider the design of putting test and production data (or, actually, 'anything and production data') in the same DB. Seems like an invitation to disaster.

Old answer below.

Are you sure you're changing the correct connection string? The connection string used by the EF is embedded inside the connection string which specifies the location of CSDL/SSDL/etc. It's common to have a "normal" connection string for use by some other part of your app (e.g., ASP.NET membership). In this case, when changing DBs you must update both of your connection strings.

Similarly, if you update the connection string at runtime then you must use specific tools for this, which understand the EF connection string format and are separate from the usual connection string builder. See the example in the link. See also this help on assigning EF connection strings.

Craig Stuntz
Yes I'm actually hitting the correct database. But problem is that the SQL being generated from EF has the "design time" database name prepended to the tablenames.In order words. My app is executing "Select * From testfoo.users" against the prodfoo database...but testfoo is the db name when i generated the model it is not on the production server. So the error is "cannot find object testfoo.users.."
`testfoo` sounds like a *schema* name, not a *database* name. DB name would look something like `dbname..schemaname.table.` Do your two DBs have different *schemas?*
Craig Stuntz
I think you are right. What I am really trying ot do is switch between different schemas (schemes) in a database. One schema is for production another schema is for test.
OK, but that's an entirely different question than what you asked. Why can't the test and production DBs (presuming they're actually different) use the same schema name?
Craig Stuntz
If the schemata are in the same DB, you *can't* switch these at runtime (except with EF 4 code-only). This is because two identically-named and structured tables in two different schemata are considered *entirely different tables.*
Craig Stuntz
+1  A: 

Sorry its not a robust answer but I found this project on codeplex ( as well as this question ) while googling around for a similar problem:

http://efmodeladapter.codeplex.com/

The features include:

  • Run-time adjustment of model schema, including:
  • Adjusting data-level table prefixes or suffixes
  • Adjusting the owner of database objects

Some code from the docs:

public partial class MyObjectContext : BrandonHaynes.ModelAdapter.EntityFramework.AdaptingObjectContext
{
        public MyObjectContext() 
     : base(myConnectionString, 
    new ConnectionAdapter(
   new TablePrefixModelAdapter("Prefix", 
    new TableSuffixModelAdapter("Suffix")), 
  System.Reflection.Assembly.GetCallingAssembly()))
 {
 ...
 }

}

Looks like its exactly what your looking for.

jfar
Hi jfar,This is interesting.. not sure if it solves my problem since I have not tested it. But if it does what the "usage" says its cool . The difference in what "schema" means in mysql vs mssql is making this question hard to test. I'm migrating to mssql from mysql which does not have this issue.
A: 

Solved my problem by moving to sql server and away from mysql.

Mysql and Mssql interpret "schemas" differently. Schemas in mysql are the same/synonyms to databases. When I created the model the schema name..which is the same as the database name is hard coded in the generated model xml. In Mssql the schema is by default "dbo" which gets hard coded but this isnt an issue since in mssql schemas and databases are different.