22

I've got an application with a working Entity model generated from an existing database. I have to point my application at a new database, with the same schema, except that the table and column names are different.

For example, my current schema has tables named like "Answer". My new schema that I need to point to has the exact same table, except it is named "tblAnswer".

My columns have also changed. Where as a column used to be called "AnswerId", it's now "zAnswerId". Don't ask about the "z" prefix, it's a long story, but it's on every column.

So, what options do I have to point this existing Entity Model (generated from the database) to a new database and adjust the mappings? I've been experimenting with some of the techniques that are used for "Code First" mappings, as outlined in this guide, but haven't had any luck. I simply don't know if this is the right approach, or if there is something that makes more sense.

Suggestions? Thanks in advance.

1 Answer 1

47

You can change the database in the web.config file.

Use data annotations to use the different table and column names.

For example:

    [Table("tblAnswer")]
    class Answer
    {
      [Column("zAnswerId")]
      public int AnswerId { get; set; }
    }
10
  • Thanks for your quick response. I'm attempting to implement your suggestion now, I'll have a response for you shortly. May 11, 2012 at 16:28
  • I've implemented your suggestions. Here's the steps I've taken: 1) Update app.config files (I'm just using a test project for this proof of concept) to point to the correct database. 2) Decorated the partial class "Answer" with the attributes you suggested. I'm currently receiving a "MetaDataException: Unable to load the specified metadata resource". From what little I know, these issues are hard to track down. Any suggestions are appreciated, but I'll start trouble shooting this issue right now. Thanks again for your help May 11, 2012 at 16:29
  • Bummer that it isn't working. You might want to grab a copy of Programming EF Code First by Julia Lerman for more comprehensive help. Let me know how you resolve it, and good luck. May 11, 2012 at 16:34
  • I've been able to get past the MetaDataException by following this article: blogs.teamb.com/craigstuntz/2010/08/13/38628 However, I'm not faced with an EntityCommandExectutionException: Invalid object name 'dbo.answers'. This makes sense, as it appears to be trying to query the table 'Answers', but it doesn't exist. I assume I need to decorate the ObjectSet property "Answers" in my , but I'm not sure how, as it's defined differently: public ObjectSet<Answer> Answers. Or, maybe this approach is incorrect. What do you think? May 11, 2012 at 16:44
  • Ok, I'm a bit confused still, as I've added the Attribute on table "Answer" to point towards "tblAnswer", but I'm going to try to recreate this scenario again as I think I might have screwed something up. Thanks again for all of your help. May 11, 2012 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.