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

by Daniel Palme 21 Comments

Feed 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

Downloads

MVCBlog.zip

Tags: .NET, ASP, C#, MVC
Related posts:
 

New comment

:

:

:

:

 

Comments

#1
Mr B 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.

03/01/2011 by Mr B

#2
Victor Nice work!! thanks for sharing this!


Regards.

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

#3
Joe 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.

04/13/2011 by Joe

#4
Daniel @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" :-)

04/13/2011 by Daniel

#5
Md.Samnur Rahman 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

07/12/2011 by Md.Samnur Rahman

#6
Daniel @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.

07/12/2011 by Daniel

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

07/13/2011 by Md.Samnur Rahman

#8
Md.Samnur Rahman 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.

07/17/2011 by Md.Samnur Rahman

#9
Daniel @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.

07/18/2011 by Daniel

#10
Md.Samnur Rahman 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.

07/20/2011 by Md.Samnur Rahman

#11
Daniel @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.

07/20/2011 by Daniel

#12
ASP .Net Web Development I appreciate your work..Even i am planning to take this step.

07/28/2011 by ASP .Net Web Development | http://www.vinfotech.com/web-development/asp.net-web-application-development.htm

#13
Kaush 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

08/09/2011 by Kaush

#14
Daniel @Kaush:
Thanks for your hint. I fixed the script.

08/10/2011 by Daniel

#15
Benny Morgan 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.

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

#16
Daniel @Benny:
Thanks for the feedback.

08/12/2011 by Daniel

#17
HS park Great Work!
Thankys for your efforts!!

09/17/2011 by HS park | http://kkojabee.tistory.com

#18
jahir Thanks. Maybe i have find something that i can work with..

09/22/2011 by jahir

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

10/27/2011 by simon

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

10/27/2011 by simon

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

10/27/2011 by Daniel