1

I am learning Entity Framework to query the database of my company. I have an ASP.NET MVC project and as of now, I have established a connection to the company's principal server database. That has given me access to all the tables and I created a separate class Library containing all the corresponding POCOs(generated automatically).

In the tutorial I was following they say to use "enable-migrations" to have the database updated with the model.

So does that mean that if I were to modify the models in my project, that would have a direct effect on the database? Since I am new to this project I do not want to do anything stupid, like altering the database. For now I just want to query the database and retrieve information, then use that information to show more or less information on a web page.

EDIT: Just as an example, I would like to show a difference between the model generated by EF and what my real table looks like. I have a table Web_Profils that contain and ID, a ProfileName and an Order (int). This DB has no primary keys or foreign keys. If there are relations, they are defined through new tables. But when EF generates all my classes, it adds ICollections, for example in Web_Profils, I have a.o virtual ICollection<Web_User_joint_Profils>Web_User_joint_Profils which is not present in the actual table, it just seems to be the relation that EF has deduced(it is the relation between Users and Profiles present in the table Web_User_joint_Profils). Now, will doing a migration affect my tables just because EF has added these collections in my model?

I've also read that it is possible to deactivate migrations using :

Database.SetInitializer(new ContextInitializerNone<YourDbContext>());

Any thoughts?

Flexabust Bergson
  • 732
  • 14
  • 34
  • 2
    Shouldn't you ask your coworkers what their database update process looks like? It differs at every company. There's no guarantee they're using EF migrations. – mason Jun 09 '17 at 15:52
  • Yeah. Seriously, read your development guidelines. The one from your company - or ask if there is no such thing. There are those among us sonciderng migrations from EF as bad as it gets (utter crap) and not using them - there are others that use them. How can we say how it is handled in your environment? In any serious project - never seen them used. – TomTom Jun 09 '17 at 16:07
  • @mason Ok, thanks. Well it's problematic here since I'm the only developer and there are no development guidelines, just my boss telling me to do this project ... I actually asked a question on SO about how to architecture my project in order to have Separation of Concerns (for the db part, and wanted to start on ADO.Net approach) and I was told to use EF for simplicity and faster development but it seems like you do not agree. Until now there was no mention of EF migrations, I don't think my company is using them currently since the intranet website was developed with VBS in 2005. – Flexabust Bergson Jun 12 '17 at 07:22
  • @TomTom Oh ok, so do you mean I can still use EF without migrations? Because I do not need to modify the model AT ALL. I just want to make some simple requests for now, but I thought that "enable-migrations" was mandatory in order for the project to work. Maybe my question was not totally on point: if I "enable-migrations" but do not touch my model or make any modification queries, will that affect my db? At a certain point I'm not sure I actually need EF, because my queries are quite simple. What I'm afraid of, is being at a point in my project where I cannot go back and this will become a pb – Flexabust Bergson Jun 12 '17 at 07:31
  • @Flexabustbergson Where did you get the idea that they were mandatory from? Check up on https://stackoverflow.com/questions/14654055/how-can-i-disable-code-first-migrations – TomTom Jun 12 '17 at 09:57
  • @TomTom Yeah I had seen this thread(also edited my question). I don't know, probably just assumed it as it was in the tutorial and couldn't find any tutorials without it(in code first approach), my bad. From your first comment, you say that it's bad to use these migrations, but do you think EF is still a good framework? – Flexabust Bergson Jun 12 '17 at 11:26
  • 1
    It is quite good - it is very primitive compared to others, but it is standard. If tehy would focus on being an ORM they would be better, but stuff like the anemic code first approach really hurt it. I use it - for 95% of my stuff. – TomTom Jun 12 '17 at 11:46

1 Answers1

1

If you update your model, you need to add a migration to your project and update your database with that migration.

Unless you do those steps after updating your model, changes will not be reflected in the database.

ISHIDA
  • 4,700
  • 2
  • 16
  • 30
  • 1
    Yeah, which means that his database actually may be good. Do not get me wrong - but until EF migrations can handle more than 0.5% of the functionality of SQL Server.. no, never touching there. There are better technologies around to manage the database. – TomTom Jun 09 '17 at 16:08
  • I agree with you. It depends on his project. Can you tell me other technologies to manage the database ? I have a code first approach project which needs to be maintained as well – ISHIDA Jun 09 '17 at 16:18
  • 1
    We use our own inhouse system that applies manually written change scripts. RedGate has a product ReadyRoll (included in VS Enterprise) that handles management and application of change scripts. DACPAC in SQL Server can do out it out of the box, but migrating data is a little complex in it (and a lot of our migrations actually also move data around). – TomTom Jun 09 '17 at 16:24
  • ReadyRoll has some cool features. I will propose this. Thank you for sharing. – ISHIDA Jun 09 '17 at 16:28
  • @ISHIDA Thank you, I will mark this as answer since it answers my question. I've added a bit of precision (edited question) on my problem though, if you have any ideas? – Flexabust Bergson Jun 12 '17 at 09:05
  • 1
    @Flexabustbergson Here we have two questions - First question - if you enable-migrations does this update database ? answer is no it will not update the database. You need to add-migration to the project- this will also not update the database, third step you need to update-database, "Performing" this command will update the database. Until you run this query in package manager console update and changes will not be reflected in database. – ISHIDA Jun 12 '17 at 13:34
  • 1
    @Flexabustbergson second question - EF has created many relations does this will affect the database. answer- once you run the "add-migation" command in your package manager console, you will see new migrations files are being added to migrations folder in your project. If you open those migration file you will have up and down methods. Up method contains what are the actions that will be performed on the database. and down method contains what should be removed from database. – ISHIDA Jun 12 '17 at 13:39
  • Ah I see, so I won't destroy the DBs then! thanks for you precise answers! – Flexabust Bergson Jun 12 '17 at 13:42
  • 1
    @Flexabustbergson - Let's say you have added new column, UP method will contain all the columns with new column and down method will contain dropping the table. So you can see what does up and down contains. You can also edit them and control them. last question - you can disable migration using that line of code or `Database.SetInitializer(new CreateDatabaseIfNotExists());` – ISHIDA Jun 12 '17 at 13:43
  • Yeah that's what I did in the end, or close: `Database.SetInitializer(null);` – Flexabust Bergson Jun 12 '17 at 13:46
  • @Flexabustbergson I would suggest you to add a key value in web.config file and use that in disabling and enabling the migrations. So, once you deploy the code to PROD. you can control the migrations with this key without making changes to code and redeploying stuff. – ISHIDA Jun 12 '17 at 13:50
  • @ISHIDA Ok will keep that in mind. I'm not sure that we will use migrations though, I'm still trying to figure out a good architecture to do things right. TomTom(see comments on first answer) told me EF migrations were not very good. And our database is not so complex even though it is big. The tables do not have any relations between each other. – Flexabust Bergson Jun 12 '17 at 13:56