Daniel Palme

Daniel Palme

.NET consultant from Germany.

Blog > ASP.NET MVC 3 - Razor based blog engine with SQL CE 4.0

ASP.NET MVC 3 - Razor based blog engine with SQL CE 4.0

About one year ago a created a blog engine which was based on ASP.NET MVC 1. Since then technologies have evolved. When ASP.NET MVC 3 was released, I decided to update my blog engine to use the new RazorViewEngine. Instead of MSSQL Express I use Microsoft SQL Server Compact 4.0 together with the new Entity Framework 'Code First' approach.

Features/Setup

The features of the blog engine are described in this post.
The setup of the blog engine is quite easy. The directory 'Setup' contains a readme explaining the necessary steps.

Requirements

Implementation

ASP.NET MVC 3

Creating views with MVC 3 is very comfortable with the new RazorViewEngine. The markup is definitely cleaner and more readable.

Validation can now be performed using ValidationAttributes in your model classes.
Together with Unobtrusive JavaScript you can display nice validation messages in your views without using inline JavaScript.

Another advantage of MVC 3 is, that in Global.asax.cs you can define attributes that are applied to all controllers. This is quite useful, since it is no more possible to forget important filters on new controllers:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
  filters.Add(new System.Web.Mvc.HandleErrorAttribute());
}

Scott Guthrie describes all the new features on his blog.

Entity Framework 'Code First'

Working with the 'Code First' approach seems to be quite great, especially when you are starting without an existing database. To get started, you have to define your entities as POCOs. Then you optionally add some constraint and validation attributes on the properties:

public class Tag
{
  [StringLength(30)]
  [Required]
  public string Name { get; set; }

  public virtual ICollection<BlogEntry> BlogEntries { get; set; }
}

When you launch the application for the first time, your schema gets automatically generated.
The ADO.NET team has a nice blog series, which describe the 'Code First' approach with great detail.

SQL CE 4.0

I decided to use the Microsoft SQL Server Compact 4.0 instead of a MSSQL Express database.
Its advantage is, that no installation is required for usage. All you need are two DLLs, which have to be placed in your bin-folder.
If you have problems to deploy your SQL CE 4.0 database, read this post: http://stackoverflow.com/questions/3468981/how-to-deploy-sql-ce-4-ctp-to-shared-hosting
Visual Studio 2010 does not yet have tooling support for managing CE databases, but you could use SQL Server Compact Toolbox instead.
With VS 2010 SP1 you can also use Visual Studio 2010 SP1 Tools for SQL Server Compact 4.0.

Screenshot

MVCBlog

Source code

The source code is available for download. You may modify and extend it as you like.

Updates

05.03.2011: Since SQL CE 4 does not support nvarchar(max), I have fixed an issue in Core.Entities.BlogEntry. If you are NOT using SQL CE 4 take a look at the TODO in this file.

14.04.2011: The final release of Entity Framework 4.0 is now supported.

10.08.2011: Dependencies now managed with NuGet

06.09.2013: I have upgraded the blog engine to ASP.NET MVC 4 and Twitter Bootstrap 3.

Downloads

MVCBlog.zip


Subscribe to RSS Feed

Tags: .NET, ASP, C#, MVC
 

Related posts

 

New comment

:

:

:

:

 

Comments

#1
Mr B

Mr B

03/01/2011

Thanks so much for sharing this! Excellent blog engine! I've adapted it for MySql. If you want the code I'll happily sent it over.
 
#2
Victor

Victor

04/08/2011 | http://www.victorfabregat.com.ar

Nice work!! thanks for sharing this!


Regards.
 
#3
Joe

Joe

04/13/2011

First - thanks so much for this! It is really great to have a blog based on MVC 3, EF and Razor !!!

Second - you really need to give your blog a name if you want it to succeed! How about "Frog Blog":-}

FYI - EF 4.1 was released to day and, unfortunately, Microsoft changed something which causes an error - "Invalid Column Name" Tab_Id, Tab_Id, BlogEntry_Id after upgrading (see http://social.msdn.microsoft.com/Forums/en-SG/adodotnetentityframework/thread/740d94a4-f575-4a5b-90e8-7533feb8708f). One way to fix the error is to rename the columns in TagBlogEntries to be: BlogEntry_Id and Tab_Id respectively.

It'll be very interesting to see how it stacks up against FunnelWeb and NBlog.

 
#4
Daniel

Daniel

04/13/2011

@Joe:
Thanks for your input. I have upgraded the database in the download. EF 4.1 is now supported.

I will think about giving my blog a name. But maybe there is a better choice than "Frog Blog" :-)
 
#5
Md.Samnur Rahman

Md.Samnur Rahman

07/12/2011

Dear
I have download ur MVCBlog.zip file
but when i run the application the following error generate

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Views/Blog/Index.cshtml

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225


Please give the solution
Thanks
shamim
 
#6
Daniel

Daniel

07/12/2011

@shamim:
In ASP.NET MVC the URL is not directly mapped to a view.
URLs are mapped to controller actions which may render any view.

Just try '/Blog' as URL, and it will work.
 
#7
Md.Samnur Rahman

Md.Samnur Rahman

07/13/2011

@Joe:
Thank you for your answer. Its works properly.
 
#8
Md.Samnur Rahman

Md.Samnur Rahman

07/17/2011

I am trying to customize MVCBLOG. When I change inline css its work properly.But When I Change external css that does not effect on the design.

Why? Please give me a solution.
 
#9
Daniel

Daniel

07/18/2011

@shamim:
I'm sorry, but since I don't know anything about your CSS customization, I can't help you. But with Firebug it will be easy to find out how which CSS rules are applied.
 
#10
Md.Samnur Rahman

Md.Samnur Rahman

07/20/2011

Dear
I am try to publish asp.net mvc3 but no success find from this publish.
My server configuration is given bellow:

This is Windows 2008 ( IIS 7.5 ) Server Hosting Info with
asp.net Framework Version 4.0, 3.5, 3.0, 2.0, ASP.net MVC2/MVC3
MSSQL 2008 R2 & 2005 , MySQL 5.0.45, PHP 5.2.13 ,
Apache Tomcat 5.5.4 SQL Server Management Tool : mylittleadmin

I am try to the following steps:
step 1: Solution Configuration change to Release mode.
step 2: Build the Complete solution
step 3: Expend the solution explorer and right click on the website.
step 4: select publish and publish web pop up dialog open.
step 5: select publish method FTP from the dropdown.
step 6: In the target location text box put the following url
http://authoritysql.com:8880
step 7: Delete all existing file prior to publish radio button checked.
step 8: Credential passive mode checked.
step 9: Finally put cpanel user name and password and the press publish.

Please give me a step by step solution.
 
#11
Daniel

Daniel

07/20/2011

@shamim:
Your server seems to be suited for the application.
But since you didn't provide any error details, I can't help you.
But Google will help you to find a solution.
 
#12
ASP .Net Web Development

ASP .Net Web Development

07/28/2011 | http://www.vinfotech.com/web-development/asp.net-web-application-development.htm

I appreciate your work..Even i am planning to take this step.
 
#13
Kaush

Kaush

08/09/2011

Please update the DB script [DB-Schema.sql] with the below to fix exceptions (Column names [[BlogEntryId] ASC, [TagId] ASC] did not match the actual table columns - could be an issue with the generation?):

/****** Object: Table [dbo].[TagBlogEntries] Script Date: 02/07/2011 19:05:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TagBlogEntries]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[TagBlogEntries](

---------------------------------------------------------------
-- The errors have been fixed here:
[BlogEntry_Id] [uniqueidentifier] NOT NULL,
[Tag_Id] [uniqueidentifier] NOT NULL,
---------------------------------------------------------------

CONSTRAINT [PK_BlogEntry2Tag] PRIMARY KEY CLUSTERED
(
[BlogEntry_Id] ASC,
[Tag_Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
 
#14
Daniel

Daniel

08/10/2011

@Kaush:
Thanks for your hint. I fixed the script.
 
#15
Benny Morgan

Benny Morgan

08/12/2011 | http://www.benny-morgan.com

Nice work. I have integrated it into my site. I still have some tweaking to do but I have not totally come to terms on how I want it to look.
I will eventully place in the footer section its orgin. It really should point back to you in a public way.
 
#16
Daniel

Daniel

08/12/2011

@Benny:
Thanks for the feedback.
 
#17
HS park

HS park

09/17/2011 | http://kkojabee.tistory.com

Great Work!
Thankys for your efforts!!
 
#18
jahir

jahir

09/22/2011

Thanks. Maybe i have find something that i can work with..
 
#19
simon

simon

10/27/2011

Thanks for your sharing, the best mvc3 engine I have seen so far.
 
#20
simon

simon

10/27/2011

I have one dumb question: how to login with a valid credential as I couldn't find a user registration form?
 
#21
Daniel

Daniel

10/27/2011

@Simon:
There is only one login, which can be changed in Web.config. See my readme (included in the download) for details.
 
#22
Nawal Paudel

Nawal Paudel

04/12/2012

Nice works, thanks for sharing us.

I wanna know how Tags and posts are mapped in TagBlogEntries tables can you please explain us that.

Thanks again.
 
#23
Daniel

Daniel

04/12/2012

@Nawal:
It's m:n mapping table. It contains the information which blog post has which tags.
Perhaps this tutorial helps you:
http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-1-introduction-and-model.aspx
 
#24
JuninZe

JuninZe

07/18/2012

Hi,
Thank you for the very beautiful work,

I wonder if you could explain to me or tell me the way I use MSSQL instead of Microsoft SQL Server Compact 4.0.

Because my hosting server does not support.
 
#25
Daniel

Daniel

07/19/2012

@JuninZe:
You must change your connection string in Web.config.
The download includes a SQL script which will setup the database.