Entity Framework 6 Refresher: Initial Setup
It has been a long time since I used Entity Framework on anything other than an brownfield project where most work consisted of adding or removing the occasional field. This series of posts is intented as a refresher as I relearn some concepts.
- Initial Setup
- Seed Data
- Mapping Files
- One-to-many mapping
- Many-to-many mapping
- Many-to-many self mapping
- One-to-one mapping
Initial setup.
In a new .Net Framwork class library, add a reference to Microsoft.EntityFramework via the NuGet package manager.
Next add a basic class
Then add a DbContext.
Next add a connection string to the app.config file that was created when you added the NuGet package.
We are now ready to set up migrations. The first step is to enable them. In the Package Manager Console (PMC from now on - this can be found under Tools | NuGet Package Manager | Package Manager Console) run the command to add migrations:
enable-migrations
This will create a /Migrations
folder with a file called configurations.cs
.
Next, create the migration for the person class (this does not need to be named “InitialMigration”)
add-migration "InitialMigration"
This will generate a migration script to add the Person
class.
Finally, run update-database
in the PMC to create the table in your database.
Leave a Comment