Hi Folks! In this post I am going to talk about the Entity Framework providers for the .NET BlogEngine I built a few days ago. My goal is to publish a stable version of it as a NuGet Package soon.
The code base comes from the official 184.108.40.206 version
What is inside?
First thing you should know is that there's a separate assembly called BlogEngine.Core.EF. So whenever the official source-code changes, I can easily merge and keep everything consistent. The new assembly then uses Entity Framework as the ORM.
Next thing is the POCO classes under BlogEngine.Core.EF.Models namespace: although the official version contains models designed for the Repository feature, they were not specifically designed for ORM. So I had to create them representing each of the database tables.
The third thing is the Hub class and the Database context class. Because the new models act as a separate layer between the database tables and the business classes (BlogEngine.Core.Post, BlogEngine.Core.Page, BlogEngine.Core.Blog, etc.), the Hub class must convert them back and forth, whenever it needs to save or retrieve information.
Some classes contain internal objects, so I used reflection to access them.
The DbContext class is pretty straight forward: it represents all the tables and inherits from the DbContext base class which also uses a connection string name (present in the webconfig).
Last, but not least, the providers themselves under the BlogEngine.Core.EF.Providers namespace:
- the EFBlogProvider, saving/deleting/retrieving blog information
- the EFMembershipProvider, taking care of user information and authentication
- and the EFRoleProvider, taking care of user authorization
How to install the files manually
The Entity Framework works differently than NHibernate when it comes to the database providers: if you are planning to use a MySQL database, a SQLite database or a SQL_CE one, you must also download/install the correspondent ADO.NET provider. In case you wanna use a SQL Server database, you don't have to because the ADO.NET already contains the provider.
NHibernate was ported from Java, so the ORM already comes with the database dialects (they are called).
Make sure you are running at least the 220.127.116.11 version of the .NET BlogEngine (otherwise the providers might not work)
Make sure you have the right database tables in your server
- Compile the source-code and copy the BlogEngine.Core.EF, the EntityFramework and the Provider DLLs into the bin directory of your website running the .NET BlogEngine
- Edit the Web.config and reference the entityFramework configuration section
- Edit the Web.config and create a connectionstring for your database called BlogEngine, but don't forget to specify the provider name or EF won't be able to connect with the database
- Edit the Web.config, adding the 3 providers and setting them as default (or use only the EF blog provider, if you prefer)
- Download and copy the ADO.NET provider DLLs into the bin directory of your website
- Edit the Web.config, adding the ADO.NET provider as a database factory provider
The easiest way to understand how the providers work is to download the source code and run the website. First, you need to create a MySQL database and configure the connectionstring (there are database scripts inside the setup website dir). Well, and this is pretty much it!
Hope you guys enjoyed it. See ya!