玛尼神兽项链,陕西代考,孙志刚事件
很久没用过ef了,最近换了公司,用的是ef框架,的确已经忘记了ef很多东西,虽说ef这东西性能不太好,但是可以满足我们的快速开发,在新的项目中我遇到了操作中间表的问题,我记得大学的时候用过,但是年代久矣,那时候又没有写博客的习惯,现在就写下来,以防又忘记了。
言归正传:
ef中间表是隐藏起来的,在ef可视化视图里面是看不到这个东东的。只能在数据库里面看到。
一共有3张表,如图下:
orders表是订单表,product是商品表,orders_product是中间表.
创建ef模型我就不说了,这个就略过了。
下面就开始操作中间表
一:新增:
public static void createfullordersbyproduct() { using (var dbcontext = new testentities()) { var order = new orders { ordertitle = "购买汽车", customername = "sss", transactiondate = datetime.now, product = new list<product>() }; var employee1 = new product { pname = "ss", orders = new list<orders>() }; var employee2 = new product { pname = "sss", orders = new list<orders>() }; dbcontext.orders.add(order); order.product.add(employee1); dbcontext.savechanges(); } }
二:删除
public static void emptyordersproduct() { using (var dbcontext = new testentities()) { //获取product为1的所有orders所有的信息 var producttoupdate = dbcontext.product.include("orders").firstordefault(x => x.id == 1); if (producttoupdate != null) { producttoupdate.orders = new list<orders>(); dbcontext.savechanges(); } else { console.writeline("查询失败"); } } }
//这也是新增
public static void addordersproduct() { using (var dbcontext = new testentities()) { var product = dbcontext.product.include("orders").firstordefault(x => x.id == 2); int[] orderlist = { 13, 14, 15, 16, 17, 18, 19 }; if (product != null) { var productorder = new hashset<int>(product.orders.select(x => x.id)); foreach (var item in dbcontext.orders) { if (productorder.contains(item.id)) { //打印出重复的orderid console.writeline("重复的id为" + item.id); console.writeline("不执行添加结果"); } else { //打印出employee表中没有id console.writeline($"即将添加的值:{item.id}"); product.orders.add(item); } } } else { console.writeline("product为空"); } dbcontext.savechanges(); } }
三:删除和修改
public static void updateinfoproductorders() { using (var dbcontext = new testentities()) { int[] orderidlist = { 13, 14, 15, 16, 17, 18, 19 }; var productorders = dbcontext.product.include("orders").firstordefault(x => x.id == 2); if (productorders != null) { //获取product中的orderlist var productorderlist = new hashset<int>(productorders.orders.select(e => e.id)); foreach (var order in dbcontext.orders) { if (orderidlist.contains(order.id)) { //判断要修改的orderid和orders表中的均包含 if (!productorderlist.contains(order.id)) { console.writeline($"修改对应的订单id表{order.id}"); productorders.orders.add(order); } } else { if (productorderlist.contains(order.id)) { console.writeline($"删除无用的订单表{order.id}"); productorders.orders.remove(order); } } } } else { console.writeline("查无的信息"); } dbcontext.savechanges(); }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Blazor server side 自家的一些开源的, 实用型项目的进度之 CEF客户端
.NET IoC模式依赖反转(DIP)、控制反转(Ioc)、依赖注入(DI)
vue+.netcore可支持业务代码扩展的开发框架 VOL.Vue 2.0版本发布
网友评论