How to connect Entity to Entity Freamework Core 2.0 Preview?
I want to create one method with which I can add / update an entity using Entity Framework 2.0-Preview.
I am using an insert update pattern.
For more information: https://msdn.microsoft.com/en-us/data/jj592676.aspx (last example)
Below is the code of the method:
public string AttachEntity(Book book)
{
_context.Entry(book).State = (book.Id == 0)
? Microsoft.EntityFrameworkCore.EntityState.Added
: Microsoft.EntityFrameworkCore.EntityState.Modified;
string msg = $"Book details {_context.Entry(book).State} Successfully";
_context.Book.Attach(book);
_context.SaveChanges();
return msg;
}
and got below error:
InvalidOperationException: The Id property for the entity type "Book" has a temporary value when trying to change the state of the object to "No change". Either set a constant value or make sure the database is configured to generate values ββfor this property.
However, an identifier is automatically generated for the books table.
and below urls for this action:
[To add] Home / AttachEntity? Id = 1 & title = 4th book & Description = New book added by attach & Price = 120 & AuthorId = 2
[To update] Home / AttachEntity? title = 4th book & Description = New book added by attach & Price = 120 & AuthorId = 2
source to share
No one has answered this question yet
Check out similar questions: