EF CodeFirst学习笔记003--如何创建表

时间:2023-03-10 07:26:07
EF CodeFirst学习笔记003--如何创建表

参考:

http://www.cnblogs.com/Wayou/archive/2012/09/20/EF_CodeFirst.html

webconfig中修改:

<connectionStrings>
     <add name="BlogEntities" connectionString="server=gwsite4;database=EfStudy;integrated security=false;User ID=sa;Password=teamplate" providerName="System.Data.SqlClient"/>
  </connectionStrings>

我的测试环境没用MVC,用的普通aspx页面:

运行Step001页面时,就会自动将你的业务类创建到远程的数据库上了.

public partial class Step001 : System.Web.UI.Page
{
BlogEntities db = new BlogEntities();
protected void Page_Load(object sender, EventArgs e)
{
foreach (Blog b in db.Blogs)
{
Response.Write(b.Title + "*");
}
Response.Write("#");
} protected void btnAdd_Click(object sender, EventArgs e)
{
Blog b = new Blog();
b.Title = "Snow test";
b.BlogTypeId = 1;
b.CreateTime = DateTime.Now;
db.Blogs.Add(b);
db.SaveChanges();
}
}