Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



Learn Now


rssbus
 


SSWUG

Reader rating:
Click here to read 3 comments about this article.
Article source: CoDe (2010 Nov/Dec)


Article Pages:  1  2 3 - Next >


Using Entity Framework in Silverlight with Visual Basic

A common requirement in building applications is the need to serialize objects and pass them across tiers between the server and the client. These objects typically hold references to each other, and managing this “graph” and tracking all the changes so that they can be properly persisted to the database can get complicated quickly.

In Visual Studio 2010 and .NET 4.0, the Entity Framework team added a template for generating self-tracking entities (STEs) to provide an easier experience building N-tier applications that need to send entities with changes between tiers. These entities are business objects that have no direct dependency on the Entity Framework, but know how to track their own changes, so that they can later be re-attached to an ObjectContext and the changes correctly merged back.

One of the most common questions we’ve received about STEs is whether these entities are compatible with other platforms such as Silverlight, and if so what are the best practices for structuring your solution. In this article, using Visual Basic 2010, we will build a Silverlight product catalog application using a WCF service and self-tracking entities as the payload between Silverlight and the service. We’ll see how new VB language features such as statement lambdas and implicit line continuation allow you to be even more productive with technologies like Silverlight and Entity Framework.

Creating the Silverlight Application with Visual Studio 2010

The challenge with using the self-tracking entities template with Silverlight is that not all Silverlight and .NET assemblies can be shared between the two platforms. In Silverlight 4.0, a new feature called assembly portability was introduced that allows some .NET assemblies to be used with Silverlight. Some of the pieces of self-tracking entities such as the DataContract attributes and the ObservableCollection(Of T) classes are not in portable assemblies so that strategy will not work here.

However, because both Silverlight and .NET support WCF data contract serialization, the data contract of your communication payload can be shared when sending messages between Silverlight and .NET. In practice, you can accomplish this if you define a class in .NET that has the same <DataContract> and <DataMember> attributes as a similar class that you define in Silverlight; the WCF serializers and deserializers take care of the rest. In the case of self-tracking entities, we will need to have two references to the self-tracking entities code in our solution: one reference from a .NET project and one reference from a Silverlight project.

To get started building the product catalog application, open Visual Studio 2010 and create a new Silverlight Application project called ProductsCatalog (Figure 1).

Click for a larger version of this image.

Figure 1: Creating a New Project.

If you don’t have the latest Silverlight developer tools installed, you will be prompted by Visual Studio to install these.

Visual Studio brings up a dialog box (Figure 2) asking how you want to host your Silverlight application. Choose to go with the defaults and host the Silverlight application in a new ASP.NET Web Application Project called ProductsCatalog.Web.

Click for a larger version of this image.

Figure 2: The New Silverlight Application dialog box.

After pressing OK, a Silverlight Application project and a .NET ASP.NET Web Application project is created in the Solution Explorer (Figure 3)

Click for a larger version of this image.

Figure 3: What you should see in Solution Explorer.

Next you have to make a decision because there are a few options for how to expose the service and the entities you pass between the service and the Silverlight application. First consider where to put the service. The options you have are to either add a service control to the ProductsCatalog.Web project, or to add a new WCF project to your solution to manage your services. The decision comes down to how you plan to host your web site and how you are already managing other services in your application or business. To keep things simple, we’ll just add the WCF service to the ProductsCatalog.Web project.

To do this, right-click on the ProductsCatalog.Web project and choose Add | New Item… In the Add New Item dialog box (Figure 4), click on the Silverlight category and then choose to add a Silverlight-enabled WCF Service. We’ve chosen to name ours CatalogService.svc.

Click for a larger version of this image.

Figure 4: Adding the WCF Service.

The final decision to make for setting up your Visual Studio solution is where to keep the Silverlight version of your entities. Again there are two options: build them into the existing Silverlight application, or create a new Silverlight class library that you can reference in your Silverlight application. If this is the only Silverlight application that will use your entities, it is fine to just build them into your existing Silverlight application project. If you plan to share the entities with other Silverlight applications or with other Silverlight class libraries, you should put them in their own class library, which is what we’ll do for our product catalog application.

To add a Silverlight class library, right-click on the Visual Studio solution and choose Add | New Project … In the Add New Project dialog box (Figure 5), select the Silverlight category and choose the Silverlight Class Library project type. In this example name it SilverlightEntities.

Click for a larger version of this image.

Figure 5: Adding the Silverlight Class Library.

When you click OK, you are prompted to choose the version of Silverlight you are targeting (Figure 6) and in this case this is Silverlight 4.

Click for a larger version of this image.

Figure 6: Choosing the Silverlight version.

You can remove the Class1.vb file that is added to this project by default.

The final step is to have the ProductsCatalog Silverlight application reference the SilverlightEntities class library. To do this, right-click on the References folder in the ProductsCatalog project (if this is not visible select the “Show All Files” button in Solution Explorer) and choose Add Reference… From the Add Reference dialog box, click on the Projects tab and select the SilverlightEntities project. This will set up the reference so you can use classes from the SilverlightEntities project within the ProductsCatalog Silverlight application.

Once you’ve completed these steps, your solution should look like Figure 7, with a ProductsCatalog project, a SilverlightEntities project, and a ProductsCatalog.Web project with a CatalogService.svc file added.

Click for a larger version of this image.

Figure 7: What you should see in Solution Explorer.

&

By: Jeff Derstadt

By: Jonathan Aneja



Article Pages:  1  2 3 - Next Page: 'Creating the Entity Framework Model' >>

Page 1: Using Entity Framework in Silverlight with Visual Basic
Page 2: Creating the Entity Framework Model
Page 3: Building the SilverlightEntities Class Library

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.7 out of 5

10 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Xojo

 

Xojo