Database Reference
In-Depth Information
Solution
Let's say that you have a Registration entity type in your model, and the Registration type has a DateTime property.
Your model might look like the one shown in Figure 3-14 .
Figure 3-14. A model with a single Registration entity type. The entity type's RegistrationDate property is a DateTime
To start, this example leverages the Code-First approach for Entity Framework. In Listing 3-27, we create the
entity classes.
Listing 3-27. Registration Entity Type
public class Registration
{
public int RegistrationId { get; set; }
public string StudentName { get; set; }
public DateTime? RegistrationDate { get; set; }
}
Next, in Listing 3-28, we create the DbContext object, which is your gateway into Entity Framework functionality
when leveraging the Code-First approach.
Listing 3-28. The DbContext Object
public class EFRecipesEntities : DbContext
{
public EFRecipesEntities()
: base("ConnectionString") {}
public DbSet<Registration> Registrations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Registration>().ToTable("Chapter3.Registration");
base.OnModelCreating(modelBuilder);
}
}
We want to group all of the registrations by just the date portion of the RegistrationDate property. You might be
tempted in LINQ to group by RegistrationDate.Date . Although this will compile, you will receive a runtime error
complaining that Date can't be translated into SQL. To group by just the date portion of the RegistrationDate, follow
the pattern in Listing 3-29.
 
Search WWH ::




Custom Search