locked
Correct way to update after db changes ? RRS feed

  • Question

  • OK, I confess... I'm late to the EF party.  Using it for the first time and very excited about it but clearly I'm missing "something".  I started my project using an existing SQL db and the "reverse engineer from db" tool.  All worked perfectly.  Now I'm making changes such as changes to existing tables, as well as adding tables.  Reading up on it, I keep seeing references to an update from database tool, but see no sign of it anywhere in my solution nor server explorer.  What I've been doing so far is rerunning the reverse engineer tool, which  has worked just fine but makes me vaguely nervous since it doesn't seem to be what's suggested anywhere... so what am I missing?


    Thanks in advance for any guidance...

    Tuesday, September 18, 2012 3:30 PM

Answers

  • Hi,

    The update from database tool is when you are using a Model First Approach (or databsae first with an edmx model). Since you reverse engineered from your database what you have is a Code First model.

    This video has a brief rundown of the different ways you can generate an EF model: http://msdn.microsoft.com/en-us/data/jj590134

    Let me know if you have more questions.

    Edit: I just realised I didn't actually tell you how to update your database with code first. What you are doing, re-running reverse engineer, is fine. But you need to be aware that you cannot modify your classes as they are going to be replaced with new ones when you re-run the tool. The alternative is to use your code first model as your source of truth for data and use code first migrations to evolve the database whenever the classes in your application change.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.


    Tuesday, September 18, 2012 3:44 PM

All replies

  • Hi,

    The update from database tool is when you are using a Model First Approach (or databsae first with an edmx model). Since you reverse engineered from your database what you have is a Code First model.

    This video has a brief rundown of the different ways you can generate an EF model: http://msdn.microsoft.com/en-us/data/jj590134

    Let me know if you have more questions.

    Edit: I just realised I didn't actually tell you how to update your database with code first. What you are doing, re-running reverse engineer, is fine. But you need to be aware that you cannot modify your classes as they are going to be replaced with new ones when you re-run the tool. The alternative is to use your code first model as your source of truth for data and use code first migrations to evolve the database whenever the classes in your application change.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.


    Tuesday, September 18, 2012 3:44 PM
  • Thanks very much for all that Glenn.  So, bearing in mind your caveats, I'm OK just rerunning the reverse engineer tool?  That's a big relief!  I admit to still being confused though, as the video (as well as all the other tutorials I've looked at) refer to the edmx as the goto way to refresh the models and my solution doesn't contain one.  All I have in the "Models" folder are cs files and in the "Code Template" folder just the tt files so I'm still puzzled, but reassured ;)

    Thanks for all your time and kind assistance!

    Tuesday, September 18, 2012 3:58 PM
  • If that works for your scenario then there is nothing wrong with running reverse engineer again.

    The intent with the Code First model, the one you have, was that you would start your application by creating the classes that you wanted to represent your data. After that you would either let Code First generate a database for you, or you would create a bunch of mappings to get that class representation to map to your database tables.

    After that you would update your classes first, then create some sort of migration to get your database in sync.

    Having said that the database first with a code first model is not an unreasonable scenario if you just like the code first way of doing things better. Just be aware that the idea of code first was to have the code drive the database, not the other way around.

    I hope that helps ease your mind and give you some background :).


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Tuesday, September 18, 2012 4:07 PM
  • At the very least I no longer have to close my eyes and hold my breath when I click on the "Reverse Engineer" tool ;)

    At risk of imposing too much on your time, I'm still confused though on the lack of an edmx file?  The video you linked to likewise ran the tool against an existing db and he wound up with the edmx  as well as tt files.  Any sense of why I was "shortchanged" ?  ;) 

    And thank you, once again, for easing my mind!

    Tuesday, September 18, 2012 4:11 PM
  • So the reverse engineer feature actually comes from the Entity Framework Power Tools, I assume that you do have the power tools and that it is the power tools that you are running?

    Inside Visual Studio there is no way to reverse engineer a code first model yet. So if you use Visual Studio to create a model you will get an EDMX file. Just go add new item and select entity data model. The wizard will then ask you if it is an existing database and let you generate the model from there.

    If you are really using a database first approach then it is probably the easiest option. Some people just prefer code first for all of their scenarios.

    So Code First has no edmx file, it's just code. The other way of doing things with a Model Fist approach does have an EDMX file and a designer that allows you to view and change things around on a nice graphical design surface.

    If you are just learning EF then you should do some stuff with both to see how they are different.


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Tuesday, September 18, 2012 4:17 PM
  • Thank you Glenn!  I'm very appreciative of all your explanations.  I finally tired of friends taunting me for still using LINQ to SQL so this is my first foray into EF and I'm still at the stage where it's all witchcraft ;) 

    Have a great day!

    Tuesday, September 18, 2012 4:28 PM