1 在config文件添加数据库连接字符串,要求name是继承DbContext的类名字。
public partial class SchoolContext : DbContext,IDisposable
<connectionStrings>
<add name="SchoolContext" connectionString="server=CHINA-BF77E9EA9\SQL2005;database=school;uid=sa;pwd=123.com"
providerName ="System.Data.SqlClient"/>
</connectionStrings>
2 public class SchoolContextInitializer :CreateDatabaseIfNotExists <SchoolContext>
{
protected override void Seed(SchoolContext context)
{
}
}
3
public partial class SchoolContext : DbContext,IDisposable
{
public SchoolContext()
{ }
modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
System.Data.Entity.Database.SetInitializer<SchoolContext>(new SchoolContextInitializer());
}
}
微软的官方博客有详细的介绍,参考 http://blogs.msdn.com/b/adonet/
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx 这个是一个Toutorial