Linq Context对象如何返回新添加对象的主键 [英] How Linq Context object return primary key of newly added object
本文介绍了Linq Context对象如何返回新添加对象的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
objectContext.Catalog.AddObject(catalog);
return catalog.CatalogId;
上面的代码段是在存储库"层中编写的,并添加了目录对象.我想将一些整数值返回给业务层中的调用函数.但是我想不调用而做:
Above code snippet is written in Repository layer and adds catalog object. I want to return some integer value back to the calling function which is in Business layer. But i want to do it without calling:
objectContext.SaveChanges();
catalog.CatalogId是表中的主键,它是一个标识字段,并设置为自动递增.
catalog.CatalogId is primary key in the table which is an identity field and set to auto increment.
推荐答案
在插入实体后,您的实体ID(在您的情况下为"CatalogId")将具有正确的值,并且您需要先调用SaveChanges()方法才能使用它.
Hi,
Your entity id, in your case "CatalogId" will have right value after inserting the entity and you need to call SaveChanges() method before you want to use it.
这篇关于Linq Context对象如何返回新添加对象的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文