Newly inserted row value to be returned using entity framework core 2.0

asp.net-core entity-framework-core

Question

I am using ExcecuteSqlCommand for update and insert, which will return an integer but i want the entire inserted or updated row. Using FromSql the updation and insertion are not happening, i am able to fetch only the items. How can i achieve this?

1
0
3/5/2019 9:13:19 AM

Accepted Answer

If you are using EF , you'd better insert/update via DbContext :

https://www.learnentityframeworkcore.com/dbcontext/adding-data

https://www.learnentityframeworkcore.com/dbcontext/modifying-data

Book book = new Book();
book.Title = "Title1";

_context.Book.Add(book);
await _context.SaveChangesAsync();

var id = book.BookId;

EF will help you fill the enitity after operations , include the new added identity column value.

0
3/6/2019 7:59:10 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow