Using Entity Framework, we can take a Code First Approach developing the data models and allow EF to translate the model classes into database tables. The properties of classes become the attributes of the tables. Defining an ID for a class will result in a primary key and referencing that ID from another class will create a foreign key relationship. The first step is to import EF to the application.
1 2 3 4 5 6 7 8 9 10 11 12 |
using System.Collections.Generic; namespace BookStore.Models { public class Author { public int AuthorId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public List<Book> Books { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 |
namespace BookStore.Models { public class Book { public int BookId { get; set; } public int AuthorId { get; set; } public Author Author { get; set; } public string Title { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System.Data.Entity; namespace BookStore.Models { public class BookStoreDb : DbContext { public BookStoreDb() { } public DbSet<Author> Authors { get; set; } public DbSet<Book> Books { get; set; } } } |
It will be useful to create Controllers and Views from the Models. With a DbContext Model in place: run build, then enable migrations from the PM Console, add an initial migration and run update-database. At this point, EF creates the database in LocalDb. See the initial create file in the Migrations folder for the operations run on update. Repeat with add-migration [name] whenever the models change and update-database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
PM> Enable-Migrations -ContextTypeName BookStoreDb Checking if the context targets an existing database... Code First Migrations enabled for project BookStore. PM> Add-Migration InitialCreate Scaffolding migration 'InitialCreate'. The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration InitialCreate' again. PM> Update-Database Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201811280425589_InitialCreate]. Applying explicit migration: 201811280425589_InitialCreate. Running Seed method. PM> |
Further control can be gained by defining the connection string in the Web.config file and modify the Configuration.cs. For example, seeding the database with preliminary data. The connection string is also the place to attach the DB if you want to see it in the App_Data folder. It will appear after building and executing the app.
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="utf-8"?> <configuration> ... <connectionStrings> <add name="BookStoreDb" connectionString="Data Source=(LocalDb)\MSSQLLocalDb; AttachDBFileName=|DataDirectory|\BookStore.mdf; Initial Catalog=BookStore; Integrated Security=SSPI" providerName="System.Data.SqlClient"/> </connectionStrings> ... </configuration> |