Database Reference
In-Depth Information
Listing 9-2. Context Class
public class Recipe1Context : DbContext
{
public Recipe1Context() : base("Recipe1ConnectionString") { }
public DbSet<Order> Orders { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>().ToTable("Chapter9.Order");
// Following configuration enables timestamp to be concurrency token
modelBuilder.Entity<Order>().Property(x => x.TimeStamp)
.IsConcurrencyToken()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
}
}
6.
Next, from Listing 9-3, add the RecipeConnectionString connection string to the
Web.Config file under the ConnectionStrings section.
Listing 9-3. Connection String for the Recipe1 Web API Service
<connectionStrings>
<add name="Recipe1ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Then add the code in Listing 9-4 to the Application_Start method in the Global.asax
file. This code will disable the Entity Framework Model Compatibility check.
7.
Listing 9-4. Disable the Entity Framework Model Compatibility Check
protected void Application_Start()
{
// Disable Entity Framework Model Compatibilty
Database.SetInitializer<Recipe1Context>(null);
...
}
8.
Finally, replace the code in the OrderController with that from Listing 9-5.
Listing 9-5. Code for the OrderController
public class OrderController : ApiController
{
// GET api/order
public IEnumerable<Order> Get()
 
Search WWH ::




Custom Search