21

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.

letsgetsilly
  • 1,156
  • 2
  • 11
  • 27

1 Answers1

45

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; }
    }
Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
  • Thanks for your quick response. I'm attempting to implement your suggestion now, I'll have a response for you shortly. – letsgetsilly May 11 '12 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 – letsgetsilly May 11 '12 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. – Jon Crowell May 11 '12 at 16:34
  • I've been able to get past the MetaDataException by following this article: http://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 Answers. Or, maybe this approach is incorrect. What do you think? – letsgetsilly May 11 '12 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. – letsgetsilly May 11 '12 at 16:56
  • Ok, so this doesn't make sense to me. I don't have table "Answers" in my database. The table is called "tblAnswer". So, I've decorated the class "Answer" with Table("tblAnswer") attribute. Isn't this supposed to resolve the mapping issue? It doesn't appear to be doing anything, so I must be missing something. – letsgetsilly May 11 '12 at 17:11
  • I don't think that the Table attribute you suggested is available on my version of Entity Framework, but rather Entity Framework 5. According to this article http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations(v=vs.110).aspx the Table attribute isn't available until Entity Framework 5 – letsgetsilly May 11 '12 at 17:16
  • Also, I think these attributes are only for "Code First" mode of working. Right now I have a generated edmx file from an existing database, which means I don't think I can use this unless I start from a code-first perspective. Does this sound correct? – letsgetsilly May 11 '12 at 17:24
  • It's actually available in 4.3 and maybe earlier. EF gets updated more frequently than .NET. Check this out: http://msdn.microsoft.com/en-us/library/gg696190(v=vs.103).aspx – Jon Crowell May 11 '12 at 17:24
  • Thanks for all of your help. I think I've been able to determine conclusively that your suggestion is specifically for Code-First approaches, not Database-generated models. Adding attributes to my generated tables seems to have no effect on the generated query. As for my other options, I think it is up to me to either rename the columns through the GUI, or implement a custom script to rename the generated tables as suggested here: http://stackoverflow.com/questions/4229812/ef4-strip-table-prefixes-on-import – letsgetsilly May 11 '12 at 19:23