MVC+EntityFrameWork codefirst

首先,在Model中编写基础类,类中通过ICollection来实现各个类之间的关系(一对多,多对多,一对多等)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
namespace MVC_Example.Models
{
    public class Class1
    {
        [DisplayName("ID")]
        public int id { get; set; }
        [DisplayName("姓名")]
        public string name { get; set; }
        [DisplayName("性别")]
        public string sex { get; set; }
        [DisplayName("年龄")]
        public int age { get; set; }
        [DisplayName("联系电话")]
        public string tel { get; set; }

        //设置类之间的关系---一对一、一对多、多对多
        public virtual ICollection<Class2> Class2 { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
namespace MVC_Example.Models
{
    public class Class2
    {
        [DisplayName("ID")]
        public int id { get; set; }
        [DisplayName("学科名称")]
        public string subjectname { get; set; }
        [DisplayName("学科成绩")]
        public float subjectscore { get; set; }
    }
}

编写数据库上下文

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVC_Example.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;


namespace MVC_Example.DAL
{
    public class Class1Context:DbContext
    {
        public readonly static string MVCContentStr = "MVC_Example";
        public DbSet<Class1> Class1Set { get;set;}
        public DbSet<Class2> Class2Set { get; set; }

        //初始化数据库上下文
        public Class1Context()
            : base(MVCContentStr)
        { 
            
        }

        //重载这个方法主要是为了通过EF生成的数据库表名称是复数形式
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

    }
}

初始化数据库表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVC_Example.Models;
using System.Data.Entity;

namespace MVC_Example.DAL
{
    public class Class1Initializer:DropCreateDatabaseIfModelChanges<Class1Context>
    {
        //初始化类对应的数据库表
        protected override void Seed(Class1Context context)
        {
            //base.Seed(context);
            var Class1 = new List<Class1>
            {
                new Class1{name="Lucy", sex="", age=23, tel="15896536553"}//初始化数据
            };
            Class1.ForEach(s => context.Class1Set.Add(s));
            context.SaveChanges();

            var Class2 = new List<Class2>
            {
                new Class2{ subjectname="外语", subjectscore=80}
            };
            Class2.ForEach(s => context.Class2Set.Add(s));
            context.SaveChanges();
        }
    }
}

最后,在Global.asax中调用数据库初始化,检测数据库初始化是否正常

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;
using System.Data.Entity;
using MVC_Example.DAL;

namespace MVC_Example
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
    // 请访问 http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);

            //调用初始化数据库
            Database.SetInitializer<Class1Context>(new Class1Initializer());
        }
    }
}

 

posted @ 2015-07-10 12:48  成神之路~  阅读(161)  评论(0)    收藏  举报