Database Reference
In-Depth Information
To use the GetCustomers stored procedure in the model, do the following.
1.
Create a new public method called GetCustomers in the DbContext subclass that takes two
string parameters and returns a collection of Customer objects, as shown in Listing 10-4.
Listing 10-4. A New Method to Return a Collection of Customer Objects
public ICollection<Customer> GetCustomers(string company, string contactTitle)
{
throw new NotImplementedException ();
}
Implement the GetCustomers() method by calling SqlQuery on the DbContext.Database
object (see Listing 10-5).
2.
Listing 10-5. DbContext Subclass with GetCustomers() Implementation
public class EF6RecipesContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public EF6RecipesContext() : base("name=EF6CodeFirstRecipesContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Types<Customer>()
.Configure(c =>
{
c.HasKey(cust => cust.CustomerId);
c.Property(cust => cust.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
c.Property(cust => cust.Name)
.HasMaxLength(50);
c.Property(cust => cust.Company)
.HasMaxLength(50);
c.Property(cust => cust.ContactTitle)
.HasMaxLength(50);
c.ToTable("Customer", "Chapter10");
});
}
 
Search WWH ::




Custom Search