开发者

How do you use a single ObjectContext for multiple types of databases?

I am struggling to find proper, accurate or well explained documentation for specific Entity Framework questions, but I am developing an application that will support multiple types of databases. Originally it was built on DLinq, but now we are adding support. We've tried to develop our own system which worked, but was slow, IQToolkit, and now Entity.

My question is, how do you use a single ObjectContext to support multiple types of databases? In the build where we used DLinq originally, there are DatabaseContext instantiations all over the place as its a per transaction item. When we tried IQToolkit, we were able to abstract the calls into our static Shell class that can do 'Shell.Database.DoSomething' where 'Shell.Database' was a database-independant object that didn't care what database it was talking to, the framework did all the work. Now we are trying to learn Entity Framework for a better solution, but what I don't want to do is have code everywhere in the application that has to instantiate a different context based on the configured database. It would be easy to have a single abstracted object that does that work under the hood, so there is only a single object being used for the database work, which I thought was the point of ObjectContext. However, we are running into small problems.

To remedy our learning, I have a test project to learn Entity with a simple Customers table like so:

How do you use a single ObjectContext for multiple types of databases?

As you can see, I am using a single EDMX model that defines the schema, and I have separate metadata files for each database type. Originally the EDMX was created by selecting an existing MySQL database The MSSQL version of Northwind has the same schema, except the MySQL schema uses varchar(36) for the GUID since there is no Guid primitive in MySQL. The way I am instantiating this is as follows:

private static void InitMySql() {
    MySqlConnectionStringBuilder builder1 = new MySqlConnectionStringBuilder();
    builder1.Server = "win-x58";
    builder1.Database = "Northwind";
    builder1.UserID = "root";
    builder1.Password = "<password>";

    EntityConnectionStringBuilder builder2 = new EntityConnectionStringBuilder();
    builder2.Metadata = @".\Metadata\MySQL\";
    builder2.Provider = "MySql.Data.MySqlClient";
    builder2.ProviderConnectionString = builder1.ToString();

    m_Context = new NorthwindObjectContext(builder2.ToString());
}

private static void InitMSSql() {
    SqlConnectionStringBuilder builder1 = new SqlConnectionStringBuilder();
    builder1.DataSource = "win-x58";
    builder1.InitialCatalog = "Northwind";
    builder1.IntegratedSecurity = true;

    EntityConnectionStringBuilder builder2 = new EntityConnectionStringBuilder();
    builder2.Metadata = @".\Metadata\MSSQL\";
    builder2.Provider = "System.Data.SqlClient";
    builder2.ProviderConnectionString = builder1.ToString();

    m_Context = new NorthwindObjectContext(builder2.ToString());
}

Now the MySQL connections, and works. I can query the database and see results. However, when I attempt to do this when its instantiated using the MSSQL connection string using this code:

foreach (Customer item in m_Context.Customers) {
    Console.Out.WriteLine(item.Name);
}

I get the following exception:

MetadataException
Schema specified is not valid. Errors: 
Entities.ssdl(10,6) : error 0040: The Type guid is not qualified 
with a namespace or alias. Only PrimitiveTypes can be used without qualification.

The SSDL for MSSQL is:

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="northwindModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
  <EntityContainer Name="northwindModelStoreContainer">
    <EntitySet Name="Customers" EntityType="northwindModel.Store.Customers" store:Type="Tables" Schema="northwind" />
  </EntityContainer>
  <EntityType Name="Customers">
    <Key>
      <PropertyRef Name="ID" />
    </Key>
    <Property Name="ID" Type="guid" Nullable="false" />
    <Property Name="Name" Type="varchar" MaxLength="45" />
    <Propert开发者_如何学Goy Name="Email" Type="varchar" MaxLength="45" />
    <Property Name="Phone" Type="varchar" MaxLength="12" />
  </EntityType>
</Schema>

So, maybe I am not understanding EF well. I could really use some good direction to try and remedy all of these design and code problems. I know this is very long and possibly subjective, but I am not sure if one part of my questions or valid because our experience with EF is so little, and I have scoured for good answers and they are incomplete, vague, or non-existant.


Guid is not a SQL Server database type; hence the error.

A Guid in SQL Server parlance is uniqueidentifier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜