c# ef框架怎么使用linq语句创建数据库
答案:1 悬赏:0 手机版
解决时间 2021-03-27 08:33
- 提问者网友:沉默菋噵
- 2021-03-26 19:20
c# ef框架怎么使用linq语句创建数据库
最佳答案
- 五星知识达人网友:掌灯师
- 2021-03-26 19:34
一个示例:创建Employee类的新对象,用适当的值填充,然后添加这个新对象到Employee集合,提交变化到数据库作为Employee表的新行。注意,主键EmployeeID是自动增值(可查看dbml代码),创建新对象时不用设置主键值。
NorthwndDataContext db = new NorthwndDataContext(@"c:\。。\northwnd.mdf");
Employee insertEmp = new Employee //创建新的Employee对象
{
LastName = "赵钱",
FirstName = "孙李",
HireDate = DateTime.Now, //当前时间
City = "北京"
};
db.Employees.InsertOnSubmit(insertEmp); //添加新对象到Employee序列
try
{
db.SubmitChanges(); //提交更新到数据库
}
catch (Exception e)
{
Console.WriteLine(e);
db.SubmitChanges(); //若出现异常仍然提交更新
}
//以下语句查看是否插入新行
var empQuery = from emp in db.Employees
select new
{
emp.EmployeeID,
emp.LastName,
emp.FirstName,
emp.HireDate,
emp.City
};
foreach (var q in empQuery)
Console.WriteLine("{0} {1} {2} {3} {4}", q.EmployeeID,
q.LastName, q.FirstName, q.HireDate, q.City);
NorthwndDataContext db = new NorthwndDataContext(@"c:\。。\northwnd.mdf");
Employee insertEmp = new Employee //创建新的Employee对象
{
LastName = "赵钱",
FirstName = "孙李",
HireDate = DateTime.Now, //当前时间
City = "北京"
};
db.Employees.InsertOnSubmit(insertEmp); //添加新对象到Employee序列
try
{
db.SubmitChanges(); //提交更新到数据库
}
catch (Exception e)
{
Console.WriteLine(e);
db.SubmitChanges(); //若出现异常仍然提交更新
}
//以下语句查看是否插入新行
var empQuery = from emp in db.Employees
select new
{
emp.EmployeeID,
emp.LastName,
emp.FirstName,
emp.HireDate,
emp.City
};
foreach (var q in empQuery)
Console.WriteLine("{0} {1} {2} {3} {4}", q.EmployeeID,
q.LastName, q.FirstName, q.HireDate, q.City);
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯