Last post Mar 10, 2015 10:41 AM by itpreneur
Mar 05, 2015 05:17 AM|LacOniC|LINK
Hi there. As an EF newbie i have some basic questions:
1. I will use an existing DB and i don't want to create tables/columns in DB when i use Code First, automatically. Should be a setting for this. Right?
2. If i use Code First and for example a table that i use changed by someone else in DB. Is there a way to notify project (and me) about this?
Mar 05, 2015 05:34 AM|pka246|LINK
You can specify the table name using Table attribute if you change the table name in DB like below:
public class Student
Or you can use Code first migration:
If you want to use Code first for existing DB then first create ADO.NET entity model for existing DB then copy all the model classes and create code first classes. I can use this approach when create model classes for existing DB.
Mar 06, 2015 07:00 AM|Vizaint|LINK
For You second question I use visual studio online: https://www.visualstudio.com/en-us/products/visual-studio-online-basic-vs.aspx
Mar 10, 2015 10:41 AM|itpreneur|LINK
Here's the best practices. Going forward thru EF6/EF7, you'll have to be familiar yourself with EF command migrations.
To answer first your queries.
#1 - Use REverse Engineer POCO to create the classes and its configuration, cut and paste/move some of the classes and move to you configuration.
Once settled to move the POCO classes, to your customize configuration and entity classes. Run your enable-migrations so that you have the initial syncing with dbo.MigrationHistory between your baseline code vs. database.
#2 - There is no automatic unless all developers follow checkin/checkout process and able to execute add-migrations, update-database
e.g. Infrastructure.Migrations.Configuration is the namespace and class where I reside my Entity Configuration
Here are the steps you want to create after generating from Database First and creating your code first which will be your baseline and probably creating your own either custom DBContext or simply reorganizing the base POCO classes.
1. Open Package Manager console. Be sure the target project for nuget is set before you run the following
2. enable-migrations -contexttypename Infrastructure.Database.DatabaseContext -Force
--- this will create migration folder and configuration.cs 3. add-migration -ConfigurationTypeName Infrastructure.Migrations.Configuration "InitialCreate"
--- class should be inherited from DbMigrationsConfiguration
---this create scafolding of up/down method
---current snapshot of code first
4. update-database -ConfigurationTypeName Infrastructure.Migrations.Configuration -script
---This will create a sql script which can be handed down to DBA.
--Simply remove -script if you want it to rectify immediately to database.
5. Execute the following script to sql server 6. update-database -TargetMigration:"201502241821449_InitialCreate" -ConfigurationTypeName Infrastructure.Migrations.Configuration -script
---this will rollback 201502241830115_AddColumn_Person_MT.cs by dropping the new column