"Entity Framework 6 Recipes" Chinese Translation--Nineth chapter EntityFramework application in N-tier architecture program (v)

Source: Internet
Author: User

Problem

You have an object that gets through WCF and you want to remove it

Solution Solutions

You have the following model

  

Our model represents the payment payment on the invoice invoice. In our application, we implemented a WCF service that handles database interactions from the client. In our case, we want to use the service to delete a payment object entity.
To keep the solution as simple as possible, we will build a WCF service library and define the model in the following steps:

1. Create a new WCF service library program named Recipe5

2. Right-click the Recipe5 project and select Add New item. Select data? Ado. NET Entity Data model. Use the wizard to add a model that contains invoices and payment tables. For the sake of simplicity, we have removed the payment navigation properties on the invoice entity. (Right-click the Payment navigation property in the Entity Framework designer for the invoice entity, and click Delete.) Right-click the timestamp attribute in the payment entity, select the attribute, and set its concurrency mode to fixed. Doing so will engage the timestamp property in concurrency control, sending values as SQL update and delete operations in subsequent WHERE clauses.

3. Change the Iservice.cs file as follows:

[ServiceContract]      Public Interface IService1    {        [OperationContract]        Payment insertpayment ();        [OperationContract]         void deletepayment (Payment Payment);    }

4. Modify the Servics.cs file as follows:

  Public classService1:iservice1 { Public voiddeletepayment (Payment Payment) {using(varContext =Newschool5entities ()) {context. Entry (Payment). State=entitystate.deleted; Context.            SaveChanges (); }        }         PublicPayment insertpayment () {using(varContext =Newschool5entities ()) {                //Delete the previous test dataContext. Database.executesqlcommand ("Delete from Payment"); Context. Database.executesqlcommand ("Delete from Invoice"); varPayment =NewPayment {Amount=99.95M, Invoice=NewInvoice {Description ="Auto Repair" }                }; Context.                Payments.add (payment); Context.                SaveChanges (); returnPayment; }        }    }

5. In order to test our service, add a new console Application project as our client in the solution. Add a service reference for the client by right-clicking the client project and selecting Add Service Reference. You may need to right-click the service item and select "Debug?" before you start the instance, you can add an instance of your service to start in the customer service reference.

class program    {        staticvoid Main (string[] args)        {            varNew  service1client ();             var payment = client. Insertpayment ();            Client. Deletepayment (payment);        }    }

"Entity Framework 6 Recipes" Chinese Translation--Nineth chapter EntityFramework application in N-tier architecture program (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.