Entity Framework using Partial classes to add business logic and validation to generated entities

With Entity Framework you can use partial classes and partial methods to add business logic or override certain methods of the generated Entity Framework entities. You can find the default MSDN information about partial classes here :
http://msdn.microsoft.com/en-us/library/wa80x488(v=vs.80).aspx

In my project I have a ADO.NET Entity Data Model which looks as following:

Entity Framework ADO.NET Entity Data Model

If we look at the generated entity code for the entity Post in the designer code:

Entity Framework partial entities

Continue reading

Entity Framework mapping insert, update and delete stored procedures and using stored procedures as functions

Entity Framework, just as LINQ to SQL, allows you to define a stored procedure for the insert, update and delete of an entity, instead of letting the EntityProvider write a custom query to the database.

I have a database called BlogData that has 3 tables and 3 stored procedures for inserting, modifying and deleting Post data:

Entity Framework Stored Procedure

1. Mapping insert, update and delete stored procedures to Entity Framework

The stored procedures look like this. The insert procedure:

SQL Server stored procedure insert

Continue reading

Using LINQ to SQL to retrieve, modify and delete data with DataContext and use mapped stored procedures

One of the predecessors of Entity Framework is LINQ to SQL. It is not deprecated, but Microsoft will no longer add features to LINQ to SQL. The ORM flagship of the .NET platform is Entity Framework and I suggest you also focus on Entity Framework if you want to use a ORM mapper to solve the impedance mismatch.

Even though LINQ to SQL is no longer being improved on, it belongs to one of the skills you have to understand for the MCTS Accessing Data with Microsoft.NET Framework 4:

LINQ to SQL

(Great thinking there Microsoft!)

I created a simple dummy database in my local SQL server to work on:

LINQ to SQL

Continue reading

Using LINQ to XML with XDocument, XElement, XAttribute and XNamespace

One of the interesting features of LINQ is LINQ to XML, if you compare the capabilies of system.xml.linq to system.xml

When creating a new XML document with LINQ to XML you will be using the system.xml.linq namespace, instead of the default system.xml namespace. The primary classes you will use to build a new XML document will be the XDocument, XElement and XAttribute classes.

Creating a new XML document can easily be done with LINQ to XML as following:

LINQ to XML create new document

As usual, when creating XML we start by creating the document which has 1 root node, which in our case is the XElement with name “Customers”. Within the rootnode Customers there will be childnodes “Customer”, which will also hold childnodes which contain information about the customer. LINQ to XML allows you to create an entire XML document by just adding elements within the constructor of the XDocument and XElement classes:

LINQ to XML XElement constructor

Continue reading

Implementing and executing C# extension methods

One of the useful features added to c# 3.0 is the introduction of Extension methods. It allows you to attach methods to sealed classes or classes that you do not have access to.

Let’s say you are working with a lot of numbers in your application and often have to check whether the numbers you are working with are even or uneven. What we are used to do is creating a helper class to extend a certain class like this:

C# Extension Methods

And we call this method by Extensions.IsEven method to do the validation whether the value is even or not:

C# Extension methods

One of the features C# 3 adds is that you can add these extension methods directly to the class you are working with. We are working with an integer, and it would be great if we could invoke the IsEven() method directly from the integer and not doing it through a Helper class. One of the advantages is that someone else in your project also doesn’t have to go dig through all the helper classes to see what methods exist in there to know what possible methods there are.

Changing the method to an extension method is a small change:

C# Extension methods

Continue reading

Entity framework Code First with Entity Framework 4.1 and System.Data.Entity.DbContext

In the previous post I created my database from the ADO.NET Data Entity Model. You can find the post here:
Entity framework creating your ADO.NET entity data model first and creating your database from your model 

It is also possible to create your database from code first, meaning creating the database from your defined classes. In that case you don’t need to model your entities anymore in your ADO.NET Entity Data Model, but you just create the classes and properties you want to work with.

To be able to work with Entity Framework Code First you need to have Entity Framework 4.1 extension installed, as this update contains the features to work with Code First. You can download Entity Framework 4.1 here:
http://www.microsoft.com/download/en/details.aspx?id=26825

1. Entity Framework Code First with System.Data.Entity.DbContext

We will create a Console Application called “EntityFramework.CodeFirst“:

Entity Framework Code First

Add a reference to System.Data.Entity and a reference to EntityFramework (this is under extensions instead of assemblies).

Entity Framework Code First

Continue reading

Entity framework creating your ADO.NET entity data model first and creating your database from your model

In this post we will cover how to use Entity Framework by model first. This means we will create the  ADO.NET entity data model first that will contain menu’s from the restaurant, with the according dishes linked to it. After we have set up our data model in visual studio, we will create the database afterwards to match the model we created in visual studio. The most common way to the derive your ADO.NET entity data model from an existing database.

If you are looking for Entity Framework 4.1 Code First instead of Model First, you can find it here:
Entity framework Code First with Entity Framework 4.1 and System.Data.Entity.DbContext
We start by creating a Console Application called “EntityFramework.CodeFirst”.

Entity Framework code first

We will add an ADO.NET Entity Data Model to our project, called Restaurant.edmx

Entity Framework ADO.NET Entity Data Model

Continue reading

WCF REST service with ODATA and Entity Framework with client context, custom operations and operation interceptors

It has been a while since I’ve made a post. Real life needed some attention as well and I had to invest into some study into Entity Framework, which is one of the topics I have to master for the upcoming ADO.NET certification. During preparation I found some information on how to construct WCF REST services with ODATA exposing data directly from an entity framework datasource.

For some this might not be interesting, but I sure as hell find this an interesting topic, considering how interoperable and scalable REST services are. In this post I’ll run through the setup and configuration of a  Restfull WCF Data Service with Entity Framework.

I’ve written 2 post before on WCF REST Services, which could be interesting to browse through quickly to understand the basics:
WCF REST service with XML / JSON response format according to Content-Type header
WCF REST service operation with JSON and XML also supporting ATOM syndication feed format

If you are looking for building REST services, this can also be done by the ASP.NET Web API:
Building and consuming REST services with ASP.NET Web API and OData support

Quoted directly from the Open Data Protocol website:

The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol(AtomPub) and JSON to provide access to information from a variety of applications, services, and stores. The protocol emerged from experiences implementing AtomPub clients and servers in a variety of products over the past several years.  OData is being used to expose and access information from a variety of sources including, but not limited to, relational databases, file systems, content management systems and traditional Web sites.

OData is consistent with the way the Web works – it makes a deep commitment to URIs for resource identification and commits to an HTTP-based, uniform interface for interacting with those resources (just like the Web).   This commitment to core Web principles allows OData to enable a new level of data integration and interoperability across a broad range of clients, servers, services, and tools.

For this post I used the AdventureWorks database that is provided by Microsoft. You can find it here:
http://msftdbprodsamples.codeplex.com/releases/view/59211 

After attaching the database to your local IIS SQL server, you should have something as following:

Adventureworks Entity Framework

Continue reading

WCF message security with username authentication and authorization provided by a membership provider

In this blog, I’ll show how to set up message security with username authentication and authorization provided by a membership provider. I will not discuss the basics about message security or self-signed certificates as I’ve reviewed those topics already.

If you do not know what self signed-certificates are, how they work and why you need them, first read this:
WCF transport security and client certificate authentication with self-signed certificates

If you do not know how to use and configure message security, first read this:
WCF message security and client certificate authentication with self-signed certificates

If you do not know what a membership provider is or how to use it, then I’m sorry, but you might want to read this as well:
MSDN walk-through: Creating a Web Site with Membership and User Login

Now we know how to work with message security, self-signed certificates and membership provider, let’s get a working WCF username authentication and authorization working with a membership provider.

My solution looks as following:

WCF Authentication and authorization with message security username

Continue reading

WCF dynamic endpoint discovery with UdpDiscoveryEndpoint and DiscoveryClient with FindCriteria and scope

In my attempt to learn the basics of Windows Communication Foundation, the WCF support for WS-Discovery is one of the topics I had to attempt, to get a bit familiar with the topic.

There are 2 possible ways of discovery:

  • Ad-hoc : UDP multicast discovery over the network. This only works for services that are on the same network as the client.
  • Managed: A discovery proxy that works as an mediator to the local network and acts as a service repository for the clients outside of the network
We will go for the Ad-Hoc UDP multicast discovery to find the endpoints of our service dynamically, instead of harcoding the service endpoint in our client code or configuration. This means this way of discovery only works within the same network, ideal for intranet situations. When you want clients outside of the network to dynamically discover endpoints within your network, you will also need to add a DiscoveryProxy, which is another story on it’s own.

1. Finding a service endpoint dynamically with Ad-Hoc Discovery and UdpDiscoveryEndpoint

For the dynamic service endpoint discovery we will need an udpDiscoveryEndpoint and a DynamicEndpoint 

Our solution looks as following:

WCF Ad hoc discovery with udpDiscoveryEndpoint

Continue reading