简体   繁体   English

实体框架:存储过程后查找返回空值

[英]Entity Framework : Find returns null after stored procedure

I have a stored procedure which inserts data into a table. 我有一个将数据插入表中的存储过程。 I'm querying immediately after the stored procedure statement with .Find to see if the insert happened, and it's returning null. 我正在使用.Find存储过程语句之后立即进行查询,以查看是否发生了插入,并且返回null。 If I query later with another DbContext , it's there, so it's as if that I need to refresh the context. 如果稍后使用另一个DbContext查询,它就在那里,就好像我需要刷新上下文一样。

Some sample code: 一些示例代码:

string name = "Awesome book";
string id = Guid.NewGuid().ToString();

// Entity Framework context
BooksDb db = new BooksDb();

// call to stored procedure 
db.insert_book(id, name);

// check if it's there
book b = db.books.Find(id); // b is null

Do I need to refresh something to I get to find the item with id that I'm looking for? 我是否需要刷新一些东西才能找到要查找的具有ID的商品?

Update: I figured it out thanks to the comments below. 更新:由于以下评论,我想通了。 I was actually looking for the id which I thought I was inserting, however the stored procedure had auto id creation. 我实际上正在寻找我以为要插入的ID,但是存储过程具有自动ID创建功能。 So basically I was querying something which didn't exist. 所以基本上我在查询不存在的东西。 :) :)

After checking the stored procedure, I figured that it had auto id creation, so I wasn't actually querying the item that I thought I had the id. 在检查了存储过程之后,我发现它具有自动ID创建功能,因此我实际上并没有查询我认为自己具有ID的项目。 SaveChanges being called on DBContext makes sure that it lights up as well. DBContext上调用SaveChanges可以确保它也亮起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM