EF中的添加数据
1.打开SQL Server 创建数据库,建好表
2.打开Visual Studio 创建AS.net项目 添加一个Web窗体 给一个button控件
在button点击事件中的代码:
protected void Button1_Click(object sender, EventArgs e)
{
Artice artices = new Artice();
artices.tide = "下雨了";
artices.contents = "下雨了不想出门耶";
artices.author = "谭蕾";
artices.Catelong = new Catelong() { name = "乐迪"};
mb.Artice.Add(artices);
mb.SaveChanges();
}
New一个实体模型: MyDBEntities1 mb = new MyDBEntities1();
在加载事件中显示对应数据:
protected void Page_Load(object sender, EventArgs e)
{
写法一
//var result = from artic in mb.Artice
// join catelo in mb.Catelong on artic.cateid equals catelo.id
// select new { Tide = artic.tide, Contents = artic.contents, Author = artic.author, aa = catelo.name };
写法二
List<Artice> artices = new List<Artice>();
artices.Add(mb.Artice.FirstOrDefault(p => p.id == 1));
this.Repeater1.DataSource = artices;
this.Repeater1.DataBind();
}