Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
v2013_S
Lesson 11:
ORM via Code First
Contents
Example 1: Installing and Referencing ............................................................................ 2
Example 2: Implementing Domain Classes ...................................................................... 4
Example 3: Implementing the Database Context.............................................................. 8
Example 4: Creating Objects ........................................................................................... 9
Example 5: Generating the Database ............................................................................. 11
Example 6: Performing Queries ..................................................................................... 14
2/15
Model First
In this approach the programmer (designer) using a graphical tool creates a metamodel and from that model generates the (2) domain classes and (3) database
tables.
Database First
In this approach the programmer (designer) generates (1) the meta-model and (2)
domain classes from the database scheme.
Code First
In this approach the programmer creates domain classes and from it generates
(1) the meta-model (which is hidden) and (2) the database tables. You can also
map classes to existing database tables.
In these exercise we will use the Code First approach.
Create a Project
1. Create a C# Library project, named Family in solution CheCosa.
2. Reference the following assemblies:
System.Data.Entity
System.ComponentModel.DataAnnotations
3/15
Implement Classes
1. Implement class Boss, as shown below:
using System;
public class Boss
{
public int ID { get; set; }
public string Name { get; set; }
public string EMail { get; set; }
public virtual Mafia Mafia { get; set; }
public override string ToString() {
return "[" + ID + " " + Name + " " + EMail + "]" + Mafia;
}
public void GiveOrder() {
Console.WriteLine("Starting operations:");
Mafia.Execute();
}
}
4/15
5/15
6/15
7/15
Implement Context
1. Implement class FamilyContext, as shown below:
using System.Data.Entity;
public class FamilyContext : DbContext
8/15
Create a Project
1. Create a C# Console project, named Program in solution CheCosa.
2. Implement class Program, as shown below:
using
using
using
using
System;
System.Collections.Generic;
System.Data.Entity;
System.Linq;
class Program
{
static Boss boss;
static Mafia mafia;
static Member a, b, c, d, e;
static Project bank, casino;
static FamilyContext context;
static
static
static
static
void
void
void
void
Main(string[] args) {
CreateObjects () { }
PrintFamily () { }
ExecuteQueries() { }
9/15
=
=
=
=
=
new
new
new
new
new
=
=
=
=
=
new
new
new
new
new
List<Project>
List<Project>
List<Project>
List<Project>
List<Project>
{
{
{
{
{
bank, casino };
bank };
casino };
bank };
casino };
10/15
Connection strings
Data Annotations
The Fluent API
Generate the Database
1. Add code to method PopulateDatabase, as shown below:
context = new FamilyContext ("Name=CheCosaDatabase");
Database.SetInitializer (
new DropCreateDatabaseAlways<FamilyContext>());
context.Bosses.Add(boss);
int noRows = context.SaveChanges();
Console.WriteLine("{0} Rows affected.", noRows);
11/15
2. Enter the following information into the dialog and click OK.
12/15
3. Create a new data diagram and add all the tables into it.
13/15
14/15
15/15