Later, to have some practical know-how, i followed this link. I found the link on this page very useful to begin with. There are various approaches to work on. Lets begin with Code First Approach for New Database -
The page is itself the best place to follow the approach. No doubt , you won't find better details here. So whats the use of this page. I got stuck in a couple of places. (as was with my first HelloWorld program
- I was using VS2010.The tutorial do not mention to place the connection string anywhere in the code. Obviously, it was not necessary to mention it but as i ran the program with exactly the same code, this exception popped up -
"The provider did not return a ProviderManifestToken string."
When i checked the details of this exception, it found that it was an error related to database connection. But where should i mention these details. First, i mentioned a connection string in the app.config.<add name="BlogContext" connectionString="Data Source=ZZZ;User ID=XXX;Password=XXX;Initial Catalog=YYY;Persist Security Info=true" providerName="System.Data.SqlClient" />
Then i update the BlogContext class by supplying it a constructor.public BlogContext() : base("Name=BlogContext") { }
The next time i ran it. It created the tables in the DB defined in the connection string. - When we change the POCO Classes in order to change the DB. We won't get the error till the code actually hits the DB. The error indicates us to use Data Migration tool to get the changes to the Database.
To do this, Open the Package Manager Console(Tools->Library Package Manager->Package Manager Console), And Fire the command -
Enable-Migration
Tip- To use intellisense, hit [tab], not [Ctrl+Space]. It's console not editor.
Then run the command below after you have changed the classes -
Add-Migration AddChange
Here, AddChange is the Name of migration which could be anything(Name it properly so that you can easily recall the changes). A file is created with the migration name where you can see all the upgrade/downgrade related changes.
Finally, run the Update-Database command to migrate the Changes to DB.
S. Shafique
Fok at RnD Team member
No comments:
Post a Comment