Notes-use EF4 poco in Silverlight

Source: Internet
Author: User

entity framework4 of. Net 4 already supports poco (Plan old CLR object). We do not need to generate an edmx file in advance to separate entity objects by writing entity classes. Here we record How to Use POCO to query data in Silverlight Program .
1. Create a project named pocosample for the Silverlight business application template.
2. Add a silvelight page under the views folder of the pocosample project. The name is MERs. XAML.
3. Add the following XAML Code to the grid Tag:

    stackpanel   margin   =" 0, 12, 0, 12 "  orientation   =" vertical " >    textblock   text   =" customer list "  style  =" {staticresource headertextstyle} " />   
     stackpanel    

4. Open the mainpage. XAML file and add the following XAML code between the home and about links:

    hyperlinkbutton   X: name   = "link3"   style  =" {staticresource linkstyle}"   navigateuri   = "/customers"   targetname   = "contentframe"   content   = "customer list"  />     rectangle   X: name   = "divider2"   style  =" {staticresource dividerstyle}"  />  

5. Add the object class customer in the hrapp project. The Code is as follows:

     Public   Class Customer { /// <Summary>          /// Manages a customer         /// </Summary>          Public   Class Customer { Public   Int Customerid {Get; set ;} Public   String Lastname {Get; set ;} Public   String Firstname {Get; set ;} Public   String Emailaddress {Get; set ;} /// <Summary>              /// Retrieves a list of MERs.              /// </Summary>             /// <Returns> </returns>              /// <Remarks>              /// In a "real" application, this code wowould              /// Call a data access component that retrieves              /// The data from a database.              /// </Remarks>              Public List <customer> retrieve () {list <customer> custlist = New List <customer> { New Customer () {customerid = 1, firstname = "Bilbo" , Lastname = "Baggins" , Emailaddress = Bb@hob.me" }, New Customer () {customerid = 2, firstname = "Frodo" , Lastname = "Baggins" , Emailaddress = Fb@hob.me" }, New Customer () {customerid = 3, firstname = "Samwise" , Lastname = "Gamgee" , Emailaddress = Sg@hob.me" }, New Customer () {customerid = 4, firstname = "Rosie" , Lastname = "Cotton" , Emailaddress = Rc@hob.me" }}; Return Custlist ;}}}

For the sake of simplicity, data is not obtained from the database or other data sources. Here we directly simulate some data.

Each user's mermerid should be unique, so the customerid must be defined as the primary key here. REFERENCE The following namespace.

 
UsingSystem. componentmodel. dataannotations;

Add the feature key to the customerid attribute.

 
[Key]Public IntCustomerid {Get; set ;}

6. In the pocosample. Web project, add a domainservice named mermerdomainservice. CS.

In the Add new domain service class window that appears, click OK.

If you use an edmx file, you can find it in the avaiable datacontext/objectcontext classes drop-down box. The generated domain service class inherits the generic class of linqtoentitiesdomainservice, if it is a custom domain service class, the generated class inherits domainservice.

7. Add the query method in customerdomainservice. CS:

 
[Enableclientaccess ()]Public ClassCustomerdomainservice: domainservice {PublicIenumerable <customer> getcustomers () {customer Cust =NewCustomer ();ReturnCust. Retrieve ();}}

8. Open customers. XAML and add the DataGrid to the textblock of customerlist. Set isreadonly to true and autogeneratecolumns to true.

 <  Stackpanel   Margin  = "0, 12, 0, 12"  Orientation  = "Vertical"   >              <  Textblock   Text  = "Customer list"   Style  = "{Staticresource headertextstyle }"  />              <  SDK: DataGrid   Autogeneratecolumns  = "True"   Isreadonly  = "True"   Height  = "156"   Name  = "Datagrid1"  Width  = "532"   />          </  Stackpanel  > 

9. On the datasource panel, you can see all data sources in the program. customerdomaincontext is the data source we just customized. Select the getcustomersquery method and javasird.

Drag the customer to the top of the DataGrid and release the mouse. In this way, the DataGrid will be bound to this data source.

Run the command to see the result:

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.