EF插入记录

using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Takeda_Regional_CN_TEEntities cn = new Takeda_Regional_CN_TEEntities();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        
        #region 插入一条记录
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                Takeda_Employee employee = new Takeda_Employee();
                employee.AD = "test";
                employee.Employee_Status = "E";
                // employee.AD = "LMX8757";
                cn.Takeda_Employee.Add(employee);
                cn.SaveChanges();
                Response.Write(employee.ID);
            }
            catch (DbEntityValidationException ex)
            {
                Response.Write(ex);
            }
        } 
        #endregion

        #region 插入多条记录
        protected void Button2_Click(object sender, EventArgs e)
        {
            List employees = new List();
            Takeda_Employee em;
            for (int i = 0; i <= 3; i++)
            {
                em = new Takeda_Employee();
                em.AD = "test" + i;
                em.Employee_Status = "E";
                employees.Add(em);
            }
            cn.Takeda_Employee.AddRange(employees);
            cn.SaveChanges();
            Response.Write(employees.Count);

        } 
        #endregion


    }
}

 

你可能感兴趣的:(c#)