Use ado.net entity to quickly create a WCF-based winform Silverlight Application

Source: Internet
Author: User

Keywords:

WCF, ado.net entit, ado.net self-trakcing entity, Silverlight, O/R Mapping

Overview

[ExampleCodeAt http://cid-56b433ad3d1871e3.office.live.com/self.aspx/.public/orderservices.zip]

Ado.net entity is an O/R Mapping Framework launched by Microsoft. It is version 1.0 in vs2008sp1 and is now released with vs2010 in version 2.0, version 2.0 is more convenient for hierarchical enterprise applications. The following is a detailed description for your reference.

In a C/S-form example, the data layer directly uses the objects created by ado.net entity to share these structures between the client and the service segment, so that the system can be quickly built, because the IDE supports visual modification, It is very convenient to modify and change the data model.

When ado.net entity is used as a data object for transmission on the client and server, what if the problem of data modification and update is solved? For example, order contains many books objects, and the client can delete, add, and modify books objects. After these operations are completed, how can they be updated to the server, in this case, you can use ado.net self-trakcing Entity Generator to automatically generate a template. This is a framework provided by IDE to automatically track changes, which is more convenient.

The detailed steps are as follows:

1. Create a project structure

Models: Data Business Objects shared between the client and the server

Web: a service that carries the WCF

Client: test the WCF Service and various interfaces.

2. Add a model:

Ordersmodel. edmx

Here is a simple object inclusion and inheritance relationship as an example.

Note:

Ø Code Generation Policy: note that this setting is set to none, Which is automatic by default. However, this automatically generated structure is difficult to update after being transferred on the client and the server, add a self-tracking object for processing

Ø link settings: For the convenience of subsequent independent queries, set the relationship between the primary and Foreign keys.

Ø cascading deletion: for example, cascade. After the order object is deleted, the associated objects are automatically deleted. [if you want to delete the objects by yourself, of course there is no problem]

3. Enable model self-tracking:

Generate a database from the model: Create a database connection and execute the generated SQL script.

The final model project is as follows:

4. Service Program

First, reference the models project and system. datat. entity. dll.

Ø objectcontext life cycle: Because the default objectcontext is delayed, you must specify the associated objects if they need to be transmitted on the client and server. Otherwise, exceptions may occur during serialization. For the life cycle, refer toArticle.

Public static void contextlazy (Action <ordersmodelcontainer> context)

{

Using (ordersmodelcontainer CTX = new ordersmodelcontainer (webconfigurationmanager. connectionstrings ["ordersmodelcontainer"]. connectionstring ))

{

CTX. contextoptions. lazyloadingenabled = false;

Context (CTX );

}

}

Ø add, delete, modify, and query a database: You can use both the LINQ and objectquery methods.

Public class orderservice: iorderservice

{

Private Static readonly int pagesize = 50;

Public void create (order)

{

Objectcontexthelpers. Context (CTX =>

{

CTX. orderset. addobject (order );

CTX. savechanges ();

});

}

Public list <order> Read (string condition, int pageindex, out int totals)

{

List <order> PS = NULL;

Int Total = 0;

Objectcontexthelpers. contextlazy (CTX =>

{

Func <order, bool> F = (p) =>

{

Return P. Name. Contains (condition );

};

Total = CTX. orderset. Where (f). Count ();

PS = CTX. orderset. Where (f). orderbydescending (P => P. ID). Skip (pageindex * pagesize). Take (pagesize). tolist <order> ();

});

Totals = total;

Return pS;

}

Public order readdetail (int id)

{

Order order = NULL;

Objectcontexthelpers. contextlazy (CTX =>

{

Order = CTX. orderset. Include ("books"). Where (O => O. ID = ID). First ();

});

Return order;

}

Public void Update (order)

{

Objectcontexthelpers. Context (CTX =>

{

CTX. orderset. applychanges (order );

CTX. savechanges ();

});

}

Public void Delete (INT orderid)

{

Order ord = This. readdetail (orderid );

Objectcontexthelpers. Context (CTX =>

{

Ord. starttracking ();

Ord. markasdeleted ();

CTX. orderset. applychanges (ORD );

CTX. savechanges ();

});

}

Public void clear ()

{

Objectcontexthelpers. Context (CTX =>

{

String SQL = @ "delete from bookset_industrybook;

Delete from bookset_itbook;

Delete from bookset;

Delete from orderset ;";

CTX. executestorecommand (SQL );

CTX. savechanges ();

});

}

}

5. Client Program

Share a models object

Private void button#click (Object sender, eventargs E)

{

Using (orderserviceclient SVC = new orderserviceclient ())

{

Order o = New Order ()

{

Name = "O1 ",

Books = new trackablecollection <book> ()

{

New itbook () {name = "it", Index = "computer "},

New industrybook () {name = "it", summary = "industry book "},

}

};

SVC. Create (O );

}

}

Private void button2_click (Object sender, eventargs E)

{

Using (orderserviceclient SVC = new orderserviceclient ())

{

Int total;

List <order> orders = SVC. Read (out total, "O", 0 );

Debug. writeline (orders. Count );

}

}

Private void button3_click (Object sender, eventargs E)

{

Using (orderserviceclient SVC = new orderserviceclient ())

{

Int total;

List <order> orders = SVC. Read (out total, "O", 0 );

Debug. writeline (orders [0]. Books. Count );

Order o = SVC. readdetail (orders [0]. ID );

Debug. writeline (O. Books. Count );

}

}

Private void button4_click (Object sender, eventargs E)

{

Using (orderserviceclient SVC = new orderserviceclient ())

{

Int total;

List <order> orders = SVC. Read (out total, "O", 0 );

Debug. writeline (orders [0]. Books. Count );

Order o = SVC. readdetail (orders [0]. ID );

O. Name + = "--";

Foreach (book B in O. Books)

{

If (B is itbook)

{

Itbook it = B As itbook;

It. index + = "--";

}

Else if (B is industrybook)

{

Industrybook ind = B As industrybook;

Ind. Summary + = "--";

}

}

/// Ä? Ä ä| or ° 2 can be? Delete shards? Except ybooks? Messages? Object?

O. Books. Add (New itbook () {name = "it2", Index = "computer "});

SVC. Update (O );

Order O1 = SVC. readdetail (orders [0]. ID );

Debug. Assert (o1.name = O. Name & O. Books. Count = O. Books. Count );

}

}

Private void button5_click (Object sender, eventargs E)

{

Using (orderserviceclient SVC = new orderserviceclient ())

{

Int total;

List <order> orders = SVC. Read (out total, "O", 0 );

Debug. writeline (orders [0]. Books. Count );

SVC. Delete (orders [0]. ID );

}

}

Private void sqlclr_click (Object sender, eventargs E)

{

Using (orderserviceclient SVC = new orderserviceclient ())

{

SVC. Clear ();

}

}

6. Silverlight PROJECT OPERATIONS

If the above method is used in the Silverlight project, it cannot be processed because models references the. net4 framework. Through the analysis of ado.net self-trakcing entity, because these classes are based on.. Net Library is implemented independently. It is confirmed through experiments that these classes can be used normally in the Silverlight runtime environment. The specific steps are as follows:

Create a Silverlight project for models (SL) and add system. runtime. serialization. dll references.

Add ordersmodel in models (SL. all files in TT (Note: Add link. Do not add physical files. After models is updated, it is automatically updated in models (SL). If a model is added or deleted, use the same method)

The silverlightapplication application references models (SL). After you add a reference to the WCF Service, you can enjoy the convenience of the Entity Framework and ado.net self-trakcing entity.

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.