ASP. NET Ajax, WCF, ADO. NET Entity Development Instance

Source: Internet
Author: User

This article from: http://www.cnblogs.com/edrp/archive/2008/09/03/1283434.html

 

Development Environment: Windows Server 2008 Enterprise, Microsoft Visual Studio 2008 SP1,. NET Framework 3.5 SP1, Microsoft SQL Server 2008

Development Architecture: ASP. NET Ajax, WCF, ADO. NET Entity Framework

Development steps:

1. Create a blank solution: jxcsln;

2. Add a class library project named jxc. dal, delete the generated class1.cs, and then reference it: system. data. entity. Otherwise, an error occurs during database connection creation and the database cannot be connected. Add the new project ADO. NET Entity date model. Name: northwinddbmodel. edmx; in the first window of the pop-up Object Model Wizard, select "generate from database". Next, set the database connection as follows:

After setting, click Next. The window shown in appears. Then, we only select the table:

Finally, click success. The generated object model is shown as follows:

3. Select the jxc. Dal class library and generate it.

4. Add a new project (Class Library Project) named jxc. BLL to delete the generated class1.cs file. Add reference, select a project, reference jxc. Dal, and reference system. Data. entity.

5. Add a new class named employeesinfo.

6. Open the employeesinfo. CS file and enter the following code:

(Using jxc. Dal is required ;)

Northwindentities employeescontent = new northwindentities ();

Public String getemployeenamebyid (INT employeeid)
{
VaR query = employeescontent. Employees. First (P => P. employeeid = employeeid );
Return query. lastname + query. firstname;
}

Public employees [] getallemployees ()
{
VaR query = from EMP in employeescontent. Employees select EMP;
Return query. toarray ();
}

 

(Time relationship, To be continued ...)

Next

7. Add a new project: ASP. NET web application and name it jxc. Web.

8. Add reference --> Project: jxc. Bll, jxc. Dal, system. Data. entity.

9. Add a new project, WCF Service, name: employeeservice. SVC; Delete the iemployeeservice. CS file, and paste the following code into the employeeservice. SVC. CS file:

 

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. runtime. serialization;
Using system. servicemodel;
Using system. text;
Using jxc. Dal;
Using jxc. BLL; // remember to reference

Namespace jxc. Web
{
[Servicecontract (namespace = "wcfservice")]
Public class employeeservice
{
Employeesinfo employeefobl = new employeesinfo ();
Int recordcount = 0;

[Operationcontract]
Public String getemployeenamebyid (INT empid)
{
Return employeefobl. getemployeenamebyid (empid );
}

[Operationcontract]
Public employees [] getallemployees ()
{
Return employeefobl. getallemployees ();
}
}
}

10. Delete the angry code in the web. config file:

<System. servicemodel>
<Behaviors>
<Servicebehaviors>
<Behavior name = "jxc. Web. employeeservicebehavior">
<Servicemetadata httpgetenabled = "true"/>
<Servicedebug includeexceptiondetailinfaults = "false"/>
</Behavior>
</Servicebehaviors>
</Behaviors>
<Services>
<Service behaviorconfiguration = "jxc. Web. employeeservicebehavior"
Name = "jxc. Web. employeeservice">
<Endpoint address = "" binding = "wshttpbinding" Contract = "jxc. Web. iemployeeservice">
<Identity>
<DNS value = "localhost"/>
</Identity>
</Endpoint>
<Endpoint address = "mex" binding = "mexhttpbinding" Contract = "imetadataexchange"/>
</Service>
</Services>
</System. servicemodel>

11. Right-click the employeeservice. SVC file and choose --> View Tag:

Code: <% @ servicehost Language = "C #" DEBUG = "true" service = "jxc. Web. employeeservice" codebehind = "employeeservice. SVC. cs" %>

Run the following code: <% @ servicehost Language = "C #" DEBUG = "true" service = "jxc. web. employeeservice "codebehind =" employeeservice. SVC. CS "factory =" system. servicemodel. activation. webscriptservicehostfactory "%>

12. Open the default. aspx window and add the Asp.net Ajax function: The scriptmanager code is as follows:
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Services>
<Asp: servicereference Path = "~ /Employeeservice. SVC "/>
</Services>
</ASP: scriptmanager>
<Br/>
<H2>
ASP. NET Ajax + WCF + ADO. NET Entity architecture instance </H2>
<P>
Enter an employee ID to call WCF and return the corresponding name! </P>
Enter the Department ID: <input id = "txtdeptid" name = "txtdeptid" type = "text"/>
<Input id = "btntransfer" onclick = "clientgetemployeeid ()" type = "button"
Value = "call"/> <span id = "Results"> </span>
</Form>
</Body>

13. Compile JavaScript Code as follows:

<SCRIPT type = "text/JavaScript">
Function clientgetemployeeid (){
VaR txtdeptid = Document. getelementbyid ("txtdeptid ");
If (isnan (txtdeptid. Value )){
Alert ('department ID must be a number! ')
Txtdeptid. Focus ();
Return;
}
VaR proxy = new wcfservice. employeeservice ();
Proxy. getemployeenamebyid (txtdeptid. Value, onsucceeded, onfailed ,"");
}
Function onsucceeded (result ){
VaR rsltelem = Document. getelementbyid ("Results ");
Rsltelem. innerhtml = result;
}
Function onfailed (error ){
VaR rsltelem = Document. getelementbyid ("Results ");
Rsltelem. innerhtml = "Call failed! ";
}
</SCRIPT>

14. reference the project in the default. aspx. CS file using:

Using jxc. BLL;
Using jxc. Dal;

 

15. The program runs as follows:

 

 

 

 

Now, the test is successful.

The following problems exist:

I. How can I call jquery instead?

2. How can I bind a web page to a gridview, whether it is called by ASP. NET Ajax or jquery?

3. How to add dependency injection and IOC to a project?

4. After ADO. NET Entity is used, the table structure in the database changes. How can we cope with changes in the project? For example, the address field of the employee table is increased from the original length of 20 to 50?

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.