Entity framework4.0 (combined use of jiuef4 and WCF Data Service)

Source: Internet
Author: User

The Data Binding of EF4 is described above. The container of EF4 can be used as the data source of the data source control. Previously, we used data binding to point to dataset, connect to the database using dataadapter, and then fill to dataset. When we use EF4 data binding, our data source control no longer relies on dataadapter to directly interact with the database, but on the EF4 container, and then the EF4 interacts with the database.

This time we will talk about the combination of EF4 and WCF data servicer: The EF4 container serves as the data provider of data service.

It doesn't matter if you don't know about the WCF data service. This is very similar to web service. It doesn't matter if you don't know about the web service either. Let's run the program first. You can see how simple it is. (In fact, when many people are getting started with new thingsDefinition. No thing can be defined in words. Even if the definition is accurate, can readers understand the definition accurately? Therefore, the simplest way is to demonstrate it. OK. If you want to study it in depth, you should at least know the direction of your efforts. This is the so-called "First Master Pole, then branches" learning process.

Okay, no more nonsense. Everyone must be bored. Haha,

========================================================== ======================================

1. Start visual studio2010: file --> new project --> Asp. NET web application. Project name: efwcfdemo.

2. delete folders and files that are not used in the project (to prevent them from being visible here): for example:

3. Right-click the efwcfdemo project and choose "add --> new item". Select ADO. NET Entity Data Modell name: northwind. For example:

4. Right-click the efwcfdemo project and choose "add à New item". Select "WCF Data Service: Name: efwcfservice. For example:

 

5. After the file is added, the efwcfservice. SVC. CS file is opened by default. If not, double-click efwcfservice. SVC to open the efwcfservice. SVC. CS file. The following code is available:

 1  public class EFWCFService : DataService< /* TODO: put your data source class name here */ >
2 {
3 // This method is called only once to initialize service-wide policies.
4 public static void InitializeService(DataServiceConfiguration config)
5 {
6 // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
7 // Examples:
8 // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
9 // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
10
11
12 config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
13 }
14 }

Change to the following code: (the generic type of the parent class of efwcfservice is set to northwindentities.

The northwindentities are actually the EF4 container class. Therefore, when the container is passed as the real parameter of the generic parameter, the Data Service retrieves data from the container. For convenience, we put *. edmx and efwcfservice. SVC files under the same namespace of the same project. You can also separate them based on your actual needs. Add the corresponding namespace when using it .)

We also added a line in the Code: config. setentitysetaccessrule ("*", entitysetrights. All); this is the access permission configured for the entityset in the EF4 container. The first parameter: "*" indicates all. Because we only have one entity set categoryset. So we will see only one categories in the browser. The second parameter: entitysetrights. All indicates that all permissions are granted. The entitysetrights enumeration value is relatively simple: it is easy to understand, so I will not talk about it much.

Efwcfservice

 1  public class EFWCFService : DataService< NorthwindEntities >
2 {
3 // This method is called only once to initialize service-wide policies.
4 public static void InitializeService(DataServiceConfiguration config)
5 {
6 // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
7 // Examples:
8 // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
9 // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
10
11 config.SetEntitySetAccessRule("*",EntitySetRights.All);
12 config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
13 }
14 }

The browser displays the following:

1   <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
2 - <service xml:base="http://localhost:1713/EFWCFService.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
3 - <workspace>
4 <atom:title>Default</atom:title>
5 - <collection href="Categories">
6 <atom:title>Categories</atom:title>
7 </collection>
8 </workspace>
9 </service>

Note: Row 5th: The href value is the URI of the current object set relative to the WCF data service project, that is, in http: // localhost: 1713/efwcfservice. append after svc/: categories is the URI of the current object set. You can try to:The source you are viewing contains frequently updated content.After subscribing to the source, the source will be added to the "common source list. The update information of the source is automatically downloaded to the computer. You can view the update information through Internet Explorer and other programs. You can go to the browser: Tools-> Internet Options-for example:

Then, you can access the uri of the categoryset.

6.In solution, right-click "-" Add-"new item and select windows form application. Name: efwcftest.

Right-click the references node under the efwcftest project and add a web service such:

Add two buttons on the form form1: A button (Name: btnfill. Display text: Fill) and a ListBox (Name: lboxcategory) such:

Add a click event to btnfill: the code is as follows:

View code

 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using EFWCFTest.EFWCFReference;
10
11 namespace EFWCFTest
12 {
13 public partial class Form1 : Form
14 {
15 public Form1()
16 {
17 InitializeComponent();
18 }
19
20 private void btnFill_Click(object sender, EventArgs e)
21 {
22 NorthwindEntities svc = new NorthwindEntities(new Uri("http://localhost:1713/EFWCFService.svc/"));
23 foreach (Category c in svc.Categories)
24 {
25 this.lboxCategory.Items.Add(string.Format("{0}::{1}",c.CategoryID,c.CategoryName));
26 }
27 }
28 }
29 }

Note:

Line 2 of the Code adds a namespace: This namespace is generated when a web reference is added, representing the namespace of the local proxy class. We directly operate on the local proxy and then use the local proxy to find and access the methods exposed by the WCF data service.

Line 2 of the Code. At this time, the northwindentities in EF4 are no longer the northwindentities. Because EF4 is not referenced in the form project, we only add a web reference pointing to the URI of the WCF data service. The data is found in EF4 by the WCF data service. Line 3 of the Code. Where does the northwindentities come from? You can locate the class in vs2010 and find that it is just a local proxy class. It is not the class mapped by *. edmx in EF4. It just happens that their names are the same by default. (Don't confuse it !!)

Set efwcftest to start the project and then run the program.

Click the fill button. For example:

If you want to query the proxy using linq as you used in the EF4 container, you can also use the following code: btnfill click event. (Assume that we need to find the entity whose categoryname starts with "B)

 1  private void btnFill_Click(object sender, EventArgs e)
2 {
3 NorthwindEntities svc = new NorthwindEntities(new Uri("http://localhost:1713/EFWCFService.svc/"));
4
5 var categories = from c in svc.Categories
6 where c.CategoryName.StartsWith("B")
7 select new { c.CategoryID, c.CategoryName };
8
9 foreach (var c in categories)
10 {
11 this.lboxCategory.Items.Add(string.Format("{0}::{1}", c.CategoryID, c.CategoryName));
12 }
13
14 }

The return type of SVC. categories is dataservicequery <t>, and the interface obtained after filtering by the LINQ expression is iqueryable <t>. Dataservicequery <t> implements the iqueryable <t> interface.

 

How is it? Does it feel that the world is better. Haha.

==================================
Okay, that's it. Good night to you and to me...

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.