DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Entity Framework Schema Translations

Entity Framework Schema Translations

Juri Strumpflohner user avatar by
Juri Strumpflohner
·
Sep. 25, 12 · Interview
Like (0)
Save
Tweet
Share
3.25K Views

Join the DZone community and get the full member experience.

Join For Free

You should have different setup environments, right. Normally something like dev, test (or staging) and production. This implies to have different DBs as well. At work we have Oracle databases and there we don't really have different DB "instances" but different schemas, depending on the environment we deploy to. This post presents an approach of allowing to dynamically "rewrite" the schema defined in the EF mappings in an easy to use way.

The Problem

When you define a mapping in Entity Framework (I'm talking about database first, don't know whether this applies to code first as well) you'll get an EntitySet section which looks something like this:
<EntitySet Name="ADDRESSES" 
    EntityType="Siag.IAM.Transversal.Entities.Store.ADDRESSES" 
    store:Type="Tables" 
    Schema="MYSCHEMA" 
    Table="ADDRESSES" />
Note that this is the xml file generated by the DevArt Entity Developer as we use that one since it has some nice features. The problem here is the hard-coded schema definition in line 4. Why? Because in the connection string you have to specify the schema name as the User Id like 
<connectionStrings>
    <add name="Entities" 
         connectionString="metadata=res...connection string='User Id=MYSCHEMA;Password=...'"
         providerName="System.Data.EntityClient" />
<connectionStrings>
So normally, when we deploy to a different environment, a web.config transformation adapts the User Id for that specific environment like changing MYSCHEMA to MYSCHEMA_TEST. Unfortunately that doesn't work because - remember - the EF configuration file has still MYSCHEMA hardcoded in the configuration file. 

Option 1: Alter session

One option is to execute a command on each connection opening, altering the used session. In Oracle 
ALTER SESSION SET CURRENT_SCHEMA=MYSCHEMA_TEST;
The drawback: you execute one additional command on each connection opening. Not probably a big overhead, but still. DevArt supports this starting from version 7 of their dotConnect driver. In there, they added anInitialization Command property defined on the connection string and which lets you specify exactly this alter session command. We currently have v 6.something ... Although we're planning to upgrade probably, I continued to search.

Option 2: Dynamically adapt the EF Metadata

The idea: Before establishing the connection, load the Entity Framework metadata and exchange the schema name.
Browsing around, I found the Entity Framework Runtime Model Adapter project on Codeplex. That sounds promising. I inspected the code, and it even had a SchemaAdapter which did the job I was looking for. But not exactly. First of all, the project seemed abandoned, with the latest update back in 2010. As a result, it only had support for the (now considered obsolete) ObjectContext rather than DbContext and finally, it adapted an approach of substituting all schema definitions which is not necessarily always desired. 

Resulting Solution: Schema Translations

So I decided to adapt the code from codeplex s.t. one could define a Schema Translations property in the connection string that looked like
Schema Translations=MYSCHEMA->MYSCHEMA_TEST,ANOTHERSCHEMA->ANOTHERSCHEMA_STAGING
Advantages of this approach: 
  • Only those schema definitions that have been explicitly specified get substituted while others remain unchanged
  • No scary if conditions in the source code to map schema definitions
  • Fully configuration based, wherefore web.config transformations work just great
I asked the author of the EFModelAdapter for the permission to publish the modified code on GitHub. Once I get a reply I'll update this post here, detailing my implementation approach. In the mean time, if you have any questions, feel free to leave a comment or contact me.
Entity Framework Schema Database Framework Translation

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Express Hibernate Queries as Type-Safe Java Streams
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid
  • ChatGPT: The Unexpected API Test Automation Help
  • Key Considerations When Implementing Virtual Kubernetes Clusters

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: