Database Reference
In-Depth Information
Our model represents Customers and their corresponding Phone Numbers. We want to put the model and
database code behind a Web API service so that any client that consumes HTTP can insert, update, and delete orders.
To create the service, perform the following steps:
1.
Create a new ASP.NET MVC 4 Web Application project, selecting the Web API template
from the Project Templates wizard. Name the project Recipe4.Service .
2.
Add a new Web API Controller to the project entitled CustomerController .
3.
Next, from Listing 9-19 add the entity base class entitled BaseEntity and the enum type
entitled TrackingState . The base class extends each entity, adding a TrackingState
property that the client is required to set when manipulating entity objects. The
TrackingState property is driven from the TrackingState enum. Note that the TrackingState
is not persisted to the database. Creating our own internal tracking state enum class lets
us keep the client free of Entity Framework dependencies that would be required if we
were to expose the Entity Framework tracking states to the client. In the DbContext file,
note how we will instruct Entity Framework not to map the TrackingState property to the
underlying database tables in the OnModelCreating method.
Listing 9-19. Entity Base Class and TrackingState Enum Type
public abstract class BaseEntity
{
protected BaseEntity()
{
TrackingState = TrackingState.Nochange;
}
public TrackingState TrackingState { get; set; }
}
public enum TrackingState
{
Nochange,
Add,
Update,
Remove,
}
4.
Next, from Listing 9-20, add the Customer and PhoneNumber entity classes.
Listing 9-20. Customer and Phone Entity Classes
public class Customer : BaseEntity
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Company { get; set; }
public virtual ICollection<Phone> Phones { get; set; }
}
 
Search WWH ::




Custom Search