Professional Documents
Culture Documents
Code First Sample
Code First Sample
To keep things simple we’re going to build a basic console application that uses Code
First to perform data access.
namespace CodeFirstNewDatabaseSample
{
class Program
{
static void Main(string[] args)
{
}
}
1
Code First Sample
3. Create a Context
Now it’s time to define a derived context, which represents a session with the database,
allowing us to query and save data. We define a context that derives from
System.Data.Entity.DbContext and exposes a typed DbSet<TEntity> for each class in our
model.
We’re now starting to use types from the Entity Framework so we need to add the
EntityFramework NuGet package.
2
Code First Sample
3
Code First Sample
4
Code First Sample
1. using System.Data.Entity;
Below the Post class in Program.cs add the following derived context.
1. public class BloggingContext : DbContext
2. {
3. public DbSet<Blog> Blogs { get; set; }
4. public DbSet<Post> Posts { get; set; }
5. }
Implement the Main method in Program.cs as shown below. This code creates a new
instance of our context and then uses it to insert a new Blog. Then it uses a LINQ query
to retrieve all Blogs from the database ordered alphabetically by Title.
1. class Program
2. {
3. static void Main(string[] args)
5
Code First Sample
4. {
5. using (var db = new BloggingContext())
6. {
7. // Create and save a new Blog
8. Console.Write("Enter a name for a new Blog: ");
9. var name = Console.ReadLine();
10.
11. var blog = new Blog { Name = name };
12. db.Blogs.Add(blog);
13. db.SaveChanges();
14.
15. // Display all Blogs from the database
16. var query = from b in db.Blogs
17. orderby b.Name
18. select b;
19.
20. Console.WriteLine("All blogs in the database:");
21. foreach (var item in query)
22. {
23. Console.WriteLine(item.Name);
24. }
25.
26. Console.WriteLine("Press any key to exit...");
27. Console.ReadKey();
28. }
29. }
30. }
Where’s My Data?
6
Code First Sample
These are just the default conventions and there are various ways to change the database
that Code First uses, more information is available in the How DbContext Discovers the
Model and Database Connection topic.
You can connect to this database using Server Explorer in Visual Studio
7
Code First Sample
8
Code First Sample
9
Code First Sample
DbContext worked out what classes to include in the model by looking at the DbSet
properties that we defined. It then uses the default set of Code First conventions to
determine table and column names, determine data types, find primary keys, etc. Later in
this walkthrough we’ll look at how you can override these conventions.
Now it’s time to make some changes to our model, when we make these changes we also
need to update the database schema. To do this we are going to use a feature called Code
First Migrations, or Migrations for short.
Migrations allows us to have an ordered set of steps that describe how to upgrade (and
downgrade) our database schema. Each of these steps, known as a migration, contains
some code that describes the changes to be applied.
The first step is to enable Code First Migrations for our BloggingContext.
10
Code First Sample
Now let’s make a change to our model, add a Url property to the Blog class:
1. public class Blog
2. {
3. public int BlogId { get; set; }
4. public string Name { get; set; }
5. public string Url { get; set; }
6.
7. public virtual List<Post> Posts { get; set; }
8. }
The Add-Migration command checks for changes since your last migration and
scaffolds a new migration with any changes that are found. We can give
migrations a name; in this case we are calling the migration ‘AddUrl’.
The scaffolded code is saying that we need to add a Url column, that can hold
string data, to the dbo.Blogs table. If needed, we could edit the scaffolded code
but that’s not required in this case.
1. namespace CodeFirstNewDatabaseSample.Migrations
2. {
3. using System;
4. using System.Data.Entity.Migrations;
5.
6. public partial class AddUrl : DbMigration
7. {
8. public override void Up()
9. {
10. AddColumn("dbo.Blogs", "Url", c => c.String());
11. }
12.
13. public override void Down()
14. {
15. DropColumn("dbo.Blogs", "Url");
16. }
17. }
18. }
11
Code First Sample
Tip: You can use the –Verbose switch when calling Update-Database to see the
SQL that is being executed against the database.
The new Url column is now added to the Blogs table in the database:
12
Code First Sample
Each package is licensed to you by its owner. Microsoft is not responsible for, nor
does it grant any licenses to, third-party packages. Some packages may include
dependencies which are governed by additional licenses. Follow the package source
(feed) URL to determine any dependencies.
6. Data Annotations
So far we’ve just let EF discover the model using its default conventions, but there are
going to be times when our classes don’t follow the conventions and we need to be able
to perform further configuration. There are two options for this; we’ll look at Data
Annotations in this section and then the fluent API in the next section.
1. public class User
2. {
3. public string Username { get; set; }
4. public string DisplayName { get; set; }
5. }
1. public class BloggingContext : DbContext
13
Code First Sample
2. {
3. public DbSet<Blog> Blogs { get; set; }
4. public DbSet<Post> Posts { get; set; }
5. public DbSet<User> Users { get; set; }
6. }
If we tried to add a migration we’d get an error saying “EntityType ‘User’ has no
key defined. Define the key for this EntityType.” because EF has no way of
knowing that Username should be the primary key for User.
We’re going to use Data Annotations now so we need to add a using statement at
the top of Program.cs
1. using System.ComponentModel.DataAnnotations;
Now annotate the Username property to identify that it is the primary key
1. public class User
2. {
3. [Key]
4. public string Username { get; set; }
5. public string DisplayName { get; set; }
6. }
14
Code First Sample
KeyAttribute
StringLengthAttribute
MaxLengthAttribute
ConcurrencyCheckAttribute
RequiredAttribute
TimestampAttribute
ComplexTypeAttribute
ColumnAttribute
TableAttribute
InversePropertyAttribute
ForeignKeyAttribute
DatabaseGeneratedAttribute
NotMappedAttribute
PM> Add-Migration 201306081900407_AddUrl
Scaffolding migration '201306081900407_AddUrl'.
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
201306081900407_AddUrl' again.
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target
database.
No pending code-based migrations.
Running Seed method.
PM>
15
Code First Sample
16
Code First Sample
17
Code First Sample
PM>
http://msdn.microsoft.com/en-us/data/jj19354
http://msdn.microsoft.com/en-us/data/ee712907
18
Code First Sample
19