1、安装VS;
2、安装NuGet;
从NuGet官方网站下载安装最新版的NuGet,网址 http://nuget.org/
安装以后重启VS2010后控制台显示如下:
每个程序包的所有者将相应程序包授权给您。Microsoft 不负责也不会授予对第三方程序包的任何许可。有些程序包可能包含受其他许可证控制的依赖项。请访问程序包源(源) URL 以确定所有依赖项。
程序包管理器控制台主机版本 2.1.31002.9028
键入“get-help NuGet”以查看所有可用的 NuGet 命令。
PM> Install-Package EntityFramework
您正在从 Microsoft 下载 EntityFramework,有关此程序包的许可协议在 http://go.microsoft.com/fwlink/?LinkId=253898&clcid=0x409 上提供。请检查此程序包是否有其他依赖项,这些依赖项可能带有各自的许可协议。您若使用程序包及依赖项,即构成您接受其许可协议。如果您不接受这些许可协议,请从您的设备中删除相关组件。
已成功安装“EntityFramework 5.0.0”。
已成功将“EntityFramework 5.0.0”添加到 MvcMusicStore。
Type 'get-help EntityFramework' to see all available Entity Framework commands.
3、安装SQLserver
4、配置数据库: web.config
<connectionStrings>
<add name="ApplicationServices" connectionString="Server=PFKJ-YFB01\PFKJ;Persist Security Info=true;Initial Catalog=UserInfoForPFKJ;Integrated Security=false;User ID=xxxx;Password=xxxx;" providerName="System.Data.SqlClient" />
<add name="CMSStore" connectionString="Server=localhost;Persist Security Info=true;Initial Catalog=CMSStore;Integrated Security=false;User ID=xx;Password=xxx;" providerName="System.Data.SqlClient" />
</connectionStrings>
5、EF初始化model Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
Database.SetInitializer<CMS_PF.Models.CMSStore>(new DropCreateDatabaseIfModelChanges<CMS_PF.Models.CMSStore>());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
6、建立seed 种子初始化数据
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace CMS_PF.Models
{
public class InitalData : DropCreateDatabaseIfModelChanges<CMSStore>
{
protected override void Seed(CMSStore context)
{
new List<DF_BillTypeMgmt>
{
new DF_BillTypeMgmt { BeginDate=DateTime.Now, BillCode="0000", BillDay=true, BillMonth=true,BillYear= true,
BillName="TEST", BillPrefix="TEST", CreatePerson="TEST", CreateTime=DateTime.Now, OrderIndex=0, Remark="TEST", SerialNumberBegin=1, SerialNumberLength=6}
}.ForEach(a => context.DF_BillTypeMgmt.Add(a));
}
}
}