Guidance to setup your own Datalayer (Entity Framework Code First (wtih Repository Pattern))

In this section we will give you some guidance to setup your own data layer in the most efficient way. The code examples are based on the Integration tests and the Northwind database. In some situations you need to modify your db environment or write mappers. Writing mappers goes beyond the scope of this documentation.

Overview

In this chapter we will handle the following topics:

  • Context
  • Entities within Entity Framework (Simple)
  • Repositories
  • Setup/Configure Repository base

Context

A DbContext holds all of the DbSets that correspond to tables within your database. You can use constructor overloading to provide a connectionstring. For our integration tests we assume that the connectionstring with key "RepositoryContext" is listed in the app.config file.

public class RepositoryContext : DbContext
    {
        public DbSet<Category> Categories { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<CustomerDemographic> CustomerDemographics  { get; set; }
        public DbSet<Employee> Employees { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Order_Detail> Order_Details { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<Region> Regions { get; set; }
        public DbSet<Shipper> Shippers { get; set; }
        public DbSet<Supplier> Suppliers { get; set; }
        public DbSet<Territory> Territories { get; set; }
    }

Entities

An entity describes all the columns of the database table and the corresponding navigation paths (for example Product -> Supplier) and the necessary additional settings. To define the primary key you need to add the using of System.ComponentModel.DataAnnotations. For the foreign key you need to add the using   System.ComponentModel.DataAnnotations.Schema. It's also possible to write your own EntityConfiguration classes. Within these classes you can denote all the properties and navigation path configurations. This type of Entity configuration is not handled in the documentation yet.

Entity Product:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    
    public partial class Product
    {
        public Product()
        {
            this.Order_Details = new HashSet<Order_Detail>();
        }
        
        [Key]
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public Nullable<int> SupplierID { get; set; }
        public Nullable<int> CategoryID { get; set; }
        public string QuantityPerUnit { get; set; }
        public Nullable<decimal> UnitPrice { get; set; }
        public Nullable<short> UnitsInStock { get; set; }
        public Nullable<short> UnitsOnOrder { get; set; }
        public Nullable<short> ReorderLevel { get; set; }
        public bool Discontinued { get; set; }
    
        public virtual Category Category { get; set; }
        public virtual ICollection<Order_Detail> Order_Details { get; set; }
        public virtual Supplier Supplier { get; set; }
    }

 

Entity Supplier:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Supplier
    {
        public Supplier()
        {
            this.Products = new HashSet<Product>();
        }
    
        [Key]
        public int SupplierID { get; set; }
        public string CompanyName { get; set; }
        public string ContactName { get; set; }
        public string ContactTitle { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string Region { get; set; }
        public string PostalCode { get; set; }
        public string Country { get; set; }
        public string Phone { get; set; }
        public string Fax { get; set; }
        public string HomePage { get; set; }
    
        public virtual ICollection<Product> Products { get; set; }
    }

Repositories

using System.Data.Entity.Repository.Testing.Entities;

namespace System.Data.Entity.Repository.IntegrationTests.Repositories
{
    public class SupplierRepository : RepositoryBase<RepositoryContext>, ISupplierRepository
    {
        public SupplierRepository()
            : base(throwExceptions: true)
        {
            base.RepositoryBaseExceptionRaised += SupplierRepository_RepositoryBaseExceptionRaised;
        }

        void SupplierRepository_RepositoryBaseExceptionRaised(Exception exception)
        {
            throw new NotImplementedException();
        }

        public Supplier FindSupplierBySupplierID(int supplierID)
        {
            Supplier result = Find<Supplier>(x => x.SupplierID == supplierID)
                .FirstOrDefault();

            return result;
        }
    }
}

Last edited Oct 9, 2012 at 10:07 AM by MMMicha, version 8

Comments

No comments yet.