Database Reference
In-Depth Information
Our model represents payments on invoices. In our application, we have implemented a WCF service to handle
the database interactions from a client. We want to delete an object, in our case a Payment entity, using the service.
To keep the solution as simple as possible, we'll build a WCF service library and define the model inside of it by doing
the following:
1.
Create a WCF Service Library by right-clicking the solution and selecting Add New Project.
Select WCF WCF Service Library. Name the WCF library Recipe5 .
Right-click the Recipe5 project, and select Add New Item. Select Data ADO.NET Entity
Data Model. Use the wizard to add a model with the Invoice and Payment tables. For
simplicity, we've removed the Payments navigation property on the Invoice entity.
(Right-click on the Payments navigation property in the Invoice entity in the Entity
Framework designer, and click Delete From Model.) Right-click the TimeStamp property
in the Payment entity, select Properties, and set its Concurrency Mode to Fixed. Doing so
will engage the TimeStamp property in concurrency control, sending the value as part of
the WHERE clauses in all subsequent SQL update and delete operations.
2.
In the IService1.cs file, change the service definition as shown in Listing 9-27.
3.
Listing 9-27. The Service Contract for Our WCF Service
[ServiceContract]
public interface IService1
{
[OperationContract]
Payment InsertPayment();
[OperationContract]
void DeletePayment(Payment payment);
}
In the Service1.cs file, implement the service as shown in Listing 9-28.
4.
Listing 9-28. The Implementation of Our Service Contract
public class Service1 : IService1
{
public Payment InsertPayment()
{
using (var context = new EFRecipesEntities())
{
// delete the previous test data
context.Database.ExecuteSqlCommand("delete from chapter9.payment");
context.Database.ExecuteSqlCommand("delete from chapter9.invoice");
var payment = new Payment { Amount = 99.95M, Invoice =
new Invoice { Description = "Auto Repair" } };
context.Payments.Add(payment);
context.SaveChanges();
return payment;
}
}
 
Search WWH ::




Custom Search