4

I got an existing database with many tables which are accessed using stored procedures only (no O/RM). I'd like to create new tables in this database using Entity Framework and the Code First approach.

Do all the tables in my existing database need to be modelized in my Entity Framework classes? Will I be able to hand-code only the new classes I need in my DbContext? Other tables really need to stay untouched and away from O/RM for the moment.

Note: I'm going to be using the latest EF5.

  • Possible duplicate : http://stackoverflow.com/questions/25620248/entity-framework-6-dbcontext-with-only-a-subset-of-all-tables/37860487#37860487 – Herr Kater Jun 16 '16 at 13:21

2 Answers2

6

As for now the Power Tools only allow you to reverse engineer all tables and views in the DB, which can be a problem if you have a big DB, with hundreds of objects, you do not want to reverse engineer.

However, I found an easy workaround for that:

Create a new technical user for the reverse engineering. To this user you only grant permission to the tables and views, that you want to be reverse engineered.

Have fun!

Reuss
  • 61
  • 3
1

You are under no obligation to map any given table with EF. If you already have a database, you may want to consider reverse-engineering your database with the EF Power Tools available from Microsoft. I did this recently with a MySQL database that I had for testing purposes and it worked quite well!

If you are new to EF an advantage is that the PowerTools write a ton of code for you, which will help you get a grasp on the syntax of Code First. You will need to modify the output but it is a great start. I really believe that this approach will give you the least headache.

The EF PowerTools can be found here: http://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d/

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
  • Thanks for helping out! I tried the Reverse Engineering method already but I get an error because of a "+" special character in the database name (heh I didn't choose that name, but I'm stuck with it). Can I hand-code my classes without using the Reverse Engineering wizard? –  Oct 17 '12 at 15:01
  • 1
    You can hand code your classes, for sure. If you cannot exclude the problematic table from the Reverse Engineer and you aren't going to create new tables that you can reverse-engineer, I would still recommend that you create a dummy DB with a couple tables and reverse engineer that. I am learning EF myself at the moment and the documentation on the MS website is a bit light. I also haven't managed to find a solid book on Code First. I learned the most from looking at how the R.E. worked and by copying that. Start there and then reinforce your knowledge with the links near the bottom of... – Levi Botelho Oct 17 '12 at 15:05
  • I'm gonna do that. Thanks for the tips! –  Oct 17 '12 at 15:11