Use ASP. NET MVC3 + EF + Jquery to create a text live broadcast system (1)

Source: Internet
Author: User
Tags connectionstrings

In People's Network: http://news.people.com.cn/GB/124421/index.html, Xinhuanet: http://www.xinhuanet.com/xhzb/index.htmand other sites have text broadcast. Yesterday, I just completed the text live broadcast system for the school's 60-day celebration. Of course, it was not done using MVC. Today, I will use ASP. NET MVC3 + EF + JQuery to complete this system. It also serves as an example in the early stage of the Entity Framework 4 in Action Reading Notes series.

Create solutions and projects

1. First, create an empty Solution

The solution name is LiveText, for example:

2. After creating a solution, you also need to create three projects, as shown in the following table:

Project name Visual Studio Project template Purpose

LiveText. Domain

C # class library

Stores the entity and logic of a domain

LiveText. WebUI

ASP. net mvc 3 Web Application

Storage controller and view

LiveText. UnitTests

Test Project

Unit Test

3. Add reference

Ninject and Moq tool libraries are used in our project. You must first add references to them, the simple method is to use the VS Package Manager Console (View your Other Windows container Package Manager Console) and enter the following command:

Install-Package Ninject-Project LiveText. WebUI

Install-Package Ninject-Project LiveText. UnitTests

Install-Package Moq-Project LiveText. UnitTests

For example:

The dependencies between projects are as follows:

Project name Tool dependency Project dependency

LiveText. Domain

No

No

LiveText. WebUI

Ninject

LiveText. Domain

LiveText. UnitTests

Ninject, Moq

LiveText. Domain, LiveText. WebUI

4. Set the dependency injection container

In the project, we use Ninject to create a controller and process dependency injection (DI ). Create an Infrastructure folder in the LiveText. WebUI project, and create a NinjectControllerFactory class in the folder. The Code is as follows:

public class NinjectControllerFactory : DefaultControllerFactory{    private IKernel ninjectKernel;    public NinjectControllerFactory()    {        ninjectKernel = new StandardKernel();        AddBindings();    }    protected override IController GetControllerInstance(RequestContext requestContext,    Type controllerType)    {        return controllerType == null        ? null        : (IController)ninjectKernel.Get(controllerType);    }    private void AddBindings()    {    }}

Then modify Global. asax as follows:

Protected void Application_Start () {AreaRegistration. registerAllAreas (); RegisterGlobalFilters (GlobalFilters. filters); RegisterRoutes (RouteTable. routes); // modify the local ControllerBuilder. current. setControllerFactory (new NinjectControllerFactory ());}

The effect is as follows:

So far, the basic framework of the project has been completed, and the database is designed below.

Design Database

EF Code-First is used here.

1. Compile entity classes

The text-based Live Broadcasting System of People's Network is divided into the categories of "New Office of the People's Network" and "International Office of the People's Republic of China". Each category contains a lot of live content. The text live broadcasting system generally requires these entity classes:

Category -- Category Title -- Title Category

Text -- Text class User -- User class

Create a folder Entities in the LiveText. Domain project and create the above four classes in the folder:

Public class Category {// <summary> // Category ID // </summary> public int CategoryID {get; set ;} /// <summary> /// category Name /// </summary> public string Name {get; set ;} /// <summary> /// Title set /// </summary> public ICollection <Title> Titles {get; set ;}}
Public class Title {// <summary> /// Title number /// </summary> public int TitleID {get; set ;} /// <summary> /// title Name /// </summary> public string Name {get; set ;} /// <summary> /// Category /// </summary> public Category {get; set ;} /// <summary> // Text set /// </summary> public ICollection <Text> Texts {get; set ;}}
Public class Text {// <summary> // Text number /// </summary> public int TextID {get; set ;} /// <summary> /// spokesman /// </summary> public string Prolocutor {get; set ;} /// <summary> /// speech /// </summary> public string ProContent {get; set ;} /// <summary> /// date /// </summary> public DateTime ProDate {get; set ;} /// <summary> /// Title /// </summary> public Title {get; set ;}}
Public class User {// <summary> // User ID /// </summary> public int UserID {get; set ;} /// <summary> /// user name /// </summary> public string UserName {get; set ;} /// <summary> /// User Password /// </summary> public string Password {get; set ;}}

2. Add EFCodeFirst

On the Package Manager Console, enter the following command:

Install-Package EFCodeFirst-Project LiveText. Domain

3. Create a context class

In the LiveText. Domain project, create a folder named Concrete and create a new LiveTextDbContext class in the folder. It inherits from System. Data. Entity. DbContext. The Code is as follows:

public class LiveTextDbContext : DbContext{    public DbSet<Category> Categories { get; set; }    public DbSet<Title> Titles { get; set; }    public DbSet<Text> Texts { get; set; }    public DbSet<User> Users { get; set; }}

4. modify Web. config

Open the Web. config of the LiveText. WebUI project and add a database connection string. The name value must be the same as the name of the context class.

  <connectionStrings>    <add name="LiveTextDbContext"          connectionString="Data Source=.;Initial Catalog=LiveText;Integrated Security=True;Pooling=False"          providerName="System.Data.SqlClient"/>  </connectionStrings>

Create a HomeController and add the following code:

public class HomeController : Controller{    LiveTextDbContext context = new LiveTextDbContext();    //    // GET: /Home/    public ActionResult Index()    {        var categories = context.Categories;        return View(categories);    }}

Add a View to the Index, for example:

Now you can run it. The running result is as follows:

Let's look at the database. EF has automatically generated a database for us. The database structure is as follows:

So far, our database design is complete.

Source code: http://files.cnblogs.com/nianming/LiveText201110131625.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.