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 :
In my project I have a ADO.NET Entity Data Model which looks as following:
If we look at the generated entity code for the entity Post in the designer code:
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:
1. Mapping insert, update and delete stored procedures to Entity Framework
The stored procedures look like this. The insert procedure:
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:
(Great thinking there Microsoft!)
I created a simple dummy database in my local SQL server to work on:
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:
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:
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:
And we call this method by Extensions.IsEven method to do the validation whether the value is even or not:
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:
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:
1. Entity Framework Code First with System.Data.Entity.DbContext
We will create a Console Application called “EntityFramework.CodeFirst“:
Add a reference to System.Data.Entity and a reference to EntityFramework (this is under extensions instead of assemblies).
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”.
We will add an ADO.NET Entity Data Model to our project, called Restaurant.edmx
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:
After attaching the database to your local IIS SQL server, you should have something as following:
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: