Database Reference
In-Depth Information
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Appointment>().ToTable("Chapter13.Appointment");
modelBuilder.Entity<Company>().ToTable("Chapter13.Company");
modelBuilder.Entity<Doctor>().ToTable("Chapter13.Doctor");
}
public DbSet<Appointment> Appointments { get; set; }
public DbSet<Company> Companies { get; set; }
public DbSet<Doctor> Doctors { get; set; }
}
Next add an App.Config class to the project, and add to it the code from Listing 13-8, inside the
ConnectionStrings section.
Listing 13-8. Connection String
<connectionStrings>
<add name="Recipe3ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
To retrieve the doctors and the companies they work for without adding them to the context object, chain the
AsNoTracking method to the query that fetches Doctors, appointments, and company information as we have done
in Listing 13-9.
Listing 13-9. Doing a Simple Query Using the AsNoTracking Method
using (var context = new Recipe3Context())
{
var company = new Company {Name = "Paola Heart Center"};
var doc1 = new Doctor {Name = "Jill Mathers", Company = company};
var doc2 = new Doctor {Name = "Robert Stevens", Company = company};
var app1 = new Appointment
{
AppointmentDate = DateTime.Parse("3/18/2010"),
Patient = "Karen Rodgers",
Doctor = doc1
};
var app2 = new Appointment
{
AppointmentDate = DateTime.Parse("3/20/2010"),
Patient = "Steven Cook",
Doctor = doc2
};
context.Doctors.Add(doc1);
context.Doctors.Add(doc2);
context.Appointments.Add(app1);
 
Search WWH ::




Custom Search