{"id":48711,"date":"2019-09-02T08:00:52","date_gmt":"2019-09-02T06:00:52","guid":{"rendered":"https:\/\/code-maze.com\/?p=48711"},"modified":"2022-01-13T10:52:16","modified_gmt":"2022-01-13T09:52:16","slug":"efcore-modifying-data","status":"publish","type":"post","link":"https:\/\/code-maze.com\/efcore-modifying-data\/","title":{"rendered":"Modifying Data with Entity Famework Core"},"content":{"rendered":"<p>Until now, we have talked about the EF Core integration into the ASP.NET Core app, how to configure nonrelational properties and how to create relationships in EF Core. Additionally, we talked about migrations and the way to use queries to fetch the data from the database.<\/p>\n<p>So now, to wrap up this series, we are going to learn about actions that modify the database content (Create, Update, Delete).<\/p>\n<p>You can download <a href=\"https:\/\/github.com\/CodeMazeBlog\/ef-core-series\/tree\/efcore-modifying-data\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">the source code<\/a> for this article on our GitHub repository.<\/p>\n<p>To see all the basic instructions and complete navigation for this series, visit <a href=\"https:\/\/code-maze.com\/entity-framework-core-series\/\" target=\"_blank\" rel=\"noopener noreferrer\">Entity Framework Core with ASP.NET Core Tutorial.<\/a><\/p>\n<h2 id=\"changetracker\"><span lang=\"EN-US\">ChangeTracker and State of the Entity in Entity Framework Core<\/span><\/h2>\n<p>Before we start modifying data with Entity Framework Core, we have to be familiar with some additional EF Core features.<\/p>\n<p>As we learned, in <a href=\"https:\/\/code-maze.com\/getting-started-with-efcore\/\" target=\"_blank\" rel=\"noopener noreferrer\">the first part of the series<\/a>, DbContext consists of only three properties: ChangeTracker, Database, and Model. We have seen the Model and Database properties in action in previous articles, so in this article, we are going to talk about the ChangeTracker and State properties.<\/p>\n<p>The ChangeTracker property provides access to change tracking information and operations about the currently loaded entity. This ability is very important when we want to execute any of the database modification operations. EF Core has this kind of information(about tracking and operations) whether we create, modify it or delete an entity.<\/p>\n<p>Furthermore, EF Core won\u2019t execute any operation until we call the SaveChanges method. So knowing what operation we want to execute is crucial to EF Core prior to calling the SaveChanges method.<\/p>\n<p>Every tracked entity has the State property attached to it. When we use the context object to load an entity without the <code>AsNoTracking<\/code> method or we change its state via the Update, Remove or Add methods, that entity will be the tracked entity. The value of the State property can be obtained with the <code>_context.Entry(our_entity).State<\/code> command.<\/p>\n<p>Here are the possible states of tracked entities:<\/p>\n<ul>\n<li>Detached \u2013 The entity isn\u2019t tracked and calling the SaveChanges method won\u2019t have any effect<\/li>\n<li>Unchanged \u2013 The entity is loaded from the database but has no changes. The SaveChanges method ignores it<\/li>\n<li>Added \u2013 The entity doesn\u2019t exist in the database and calling the SaveChanges method will add it to the database<\/li>\n<li>Modified \u2013 The entity exists in the database and has been modified, therefore, calling the SaveChanges method will modify it in the database<\/li>\n<li>Deleted \u2013 The entity exists in the database and as soon as we call the SaveChanges method it will be removed from the database<\/li>\n<\/ul>\n<p>Now, when we know all this stuff, we can continue towards the Create, Update and Delete operations.<\/p>\n<h2 id=\"creatingrows\">Creating Rows \u2013 Modifying Data with Add and AddRange methods<\/h2>\n<p>We can create new rows in the database by using the Add and AddRange methods. The Add method will modify the State of the single entity where the AddRange can do the same but for the multiple entities. Let\u2019s see the Add method in action after we send the student object from Postman:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPost]\r\npublic IActionResult Post([FromBody] Student student)\r\n{\r\n    if (student == null)\r\n        return BadRequest();\r\n\r\n    if (!ModelState.IsValid)\r\n        return BadRequest();\r\n\r\n    _context.Add(student);\r\n    _context.SaveChanges();\r\n\r\n    return Created(\"URI of the created entity\", student);\r\n}\r\n<\/pre>\n<p>In this action, we use the HttpPost attribute to mark it as a POST action. Additionally, we load the student entity with the FromBody attribute and add two validation checks in our code.<\/p>\n<p>All of these are ASP.NET Core related, so if you are not familiar with these concepts we strongly suggest visiting the <a href=\"https:\/\/code-maze.com\/net-core-web-development-part6\/\" target=\"_blank\" rel=\"noopener noreferrer\">ASP.NET Core Web API \u2013 Post, Put, Delete<\/a> article. This article will also help you if you are not familiar with the process of sending the POST requests from Postman.<\/p>\n<p>After these checks, we call the <code>Add<\/code> method to change the state of the entity to Added and call the <code>SaveChanges<\/code> method to create a new row in the database. The last line of code will return the created entity (with its id) to the client:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/31-Returned-entity-after-adding.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48713\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/31-Returned-entity-after-adding.png\" alt=\"New Added entity - Modifying Data\" width=\"360\" height=\"140\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/31-Returned-entity-after-adding.png 360w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/31-Returned-entity-after-adding-300x117.png 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/a><\/p>\n<p>This is the result in the database:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/32-Created-entity-in-db.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48714\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/32-Created-entity-in-db.png\" alt=\"Database result\" width=\"505\" height=\"130\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/32-Created-entity-in-db.png 505w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/32-Created-entity-in-db-300x77.png 300w\" sizes=\"auto, (max-width: 505px) 100vw, 505px\" \/><\/a><\/p>\n<h3>Tracking Changes when Adding an Entity<\/h3>\n<p>If we modify our code slightly, we can inspect the way that the entity\u2019s state is changed during this action. Of course, the additional code is not required to save the entity to the database, it is here just for learning purposes:<\/p>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-highlight=\"12,16,20\" data-enlighter-title=\"\">[HttpPost]\r\npublic IActionResult Post([FromBody] Student student)\r\n{\r\n    if (student == null)\r\n        return BadRequest();\r\n\r\n    if (!ModelState.IsValid)\r\n        return BadRequest();\r\n\r\n    var stateBeforeAdd = _context.Entry(student).State;\r\n\r\n    _context.Add(student);\r\n\r\n    var stateAfterAdd = _context.Entry(student).State;\r\n\r\n    _context.SaveChanges();\r\n\r\n    var stateAfterSaveChanges = _context.Entry(student).State;\r\n\r\n    return Created(&quot;URI of the created entity&quot;, student);\r\n}\r\n<\/pre><\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/33-States-for-Add-method.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48715\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/33-States-for-Add-method.png\" alt=\"state check while adding an entity - Modifying data\" width=\"727\" height=\"128\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/33-States-for-Add-method.png 727w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/33-States-for-Add-method-300x53.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><\/a><\/p>\n<p>As we can see, before we add our entity to the db it has the <code>Detached<\/code> state. As soon as we use the <code>Add<\/code> method, it has the <code>Added<\/code> state. Finally, after the <code>SaveChanges<\/code> method, it has the <code>Unchanged<\/code> state.<\/p>\n<h3>Using the AddRange Method<\/h3>\n<p>When we want to add multiple rows in the database, we use the <code>AddRange<\/code> method. It is the same procedure just a different method:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPost(\"postrange\")]\r\npublic IActionResult PostRange([FromBody] IEnumerable&lt;Student&gt; students)\r\n{\r\n    \/\/additional checks\r\n\r\n    _context.AddRange(students);\r\n    _context.SaveChanges();\r\n\r\n    return Created(\"URI is going here\", students);\r\n}\r\n<\/pre>\n<p>Another important thing to mention here is that the <code>SaveChanges<\/code> method executes the required operation but it also returns the number of affected rows. So if we need to check how many rows are affected by our operation, we can always return the result of the SaveChanges method:<\/p>\n<p><code>var result = _context.SaveChanges();<\/code><\/p>\n<h2 id=\"addingrelatedentities\">Adding Related Entities to the Database<\/h2>\n<p>Now we are going to learn how to include relationships while adding the main entity to the database. To demonstrate that, we are going to modify our first example a bit:<\/p>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-highlight=\"7-12\" data-enlighter-title=\"\">[HttpPost]\r\npublic IActionResult Post([FromBody] Student student)\r\n{\r\n    \/\/validation code goes here\r\n\r\n    student.StudentDetails = new StudentDetails\r\n    {\r\n        Address = &quot;Added Address&quot;,\r\n        AdditionalInformation = &quot;Additional information added&quot;\r\n    };\r\n\r\n    _context.Add(student);\r\n    _context.SaveChanges();\r\n\r\n    return Created(&quot;URI of the created entity&quot;, student);\r\n}\r\n<\/pre><\/p>\n<p>As we can see from the code above, we are only adding the <code>student<\/code> object to the application\u2019s context object but the <code>studentDetails<\/code> object is also added to the database:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/34-Add-method-with-relationship.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48716\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/34-Add-method-with-relationship.png\" alt=\"Add method with relationship - Modifying data\" width=\"756\" height=\"302\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/34-Add-method-with-relationship.png 756w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/34-Add-method-with-relationship-300x120.png 300w\" sizes=\"auto, (max-width: 756px) 100vw, 756px\" \/><\/a><\/p>\n<p>And we can check the response from the application:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/35-Add-method-with-relationship-response.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48717\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/35-Add-method-with-relationship-response.png\" alt=\"response for the add method\" width=\"438\" height=\"202\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/35-Add-method-with-relationship-response.png 438w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/35-Add-method-with-relationship-response-300x138.png 300w\" sizes=\"auto, (max-width: 438px) 100vw, 438px\" \/><\/a><\/p>\n<p>It is important to notice that we are not adding Id value manually for any entity, EF Core is in charge of that. You can see in the database that the GUID values have been generated.<\/p>\n<p>Excellent. We can do the same thing with one-to-many or many-to-many relationships.<\/p>\n<h2 id=\"updaterows\">Updating Rows in the Database<\/h2>\n<p>There are two ways to update rows in the database. With Connected Update and with Disconnected Update. The difference is that with the connected update we use the same context object to load and modify entity. With the disconnected update, this is not the case. Either we use different context objects or we receive an object from a client that has all the properties as the entity in db, so we can update it without loading action first.<\/p>\n<p>While working with the connected update there are three main steps to execute:<\/p>\n<ul>\n<li>Read data from the database (with or without its relationships)<\/li>\n<li>Change one or more properties<\/li>\n<li>Call the SaveChanges method<\/li>\n<\/ul>\n<p><em>In the following examples, we are going to leave out the additional validations checks (for the sake of simplicity) because the validation is related to ASP.NET Core and you can read more about that in the <a href=\"https:\/\/code-maze.com\/net-core-web-development-part6\/\" target=\"_blank\" rel=\"noopener noreferrer\">ASP.NET Core Web API \u2013 Post, Put, Delete<\/a> article. <\/em><\/p>\n<p>So, let\u2019s see the connected update in action when we send the student object from a client with the <code>Name<\/code> property updated:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPut(\"{id}\")]\r\npublic IActionResult PUT (Guid id, [FromBody] Student student)\r\n{\r\n    var dbStudent = _context.Students\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    dbStudent.Age = student.Age;\r\n    dbStudent.Name = student.Name;\r\n    dbStudent.IsRegularStudent = student.IsRegularStudent;\r\n\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>We can see all three steps in the code above. We load the student object based on its id, then map the properties from the client\u2019s updated student, which sets the state of the entity to the <code>Modified<\/code> and finally, we call <code>SaveChanges<\/code>. Even though we have changed only the <code>Name<\/code> property, we have to map all the properties because we don\u2019t know what has been modified on the client-side exactly.<\/p>\n<p>EF Core, on the other hand, has the information on what exactly has been modified.<\/p>\n<p>When we load our entity, EF Core starts tracking it and at that point the State is <code>Unchanged<\/code>. Once we modify any property from a loaded entity it changes the <code>State<\/code> to <code>Modified<\/code>. Finally, after the <code>SaveChanges<\/code> method, the State is reverted to <code>Unchanged<\/code>.<\/p>\n<p>Once we send a request to this PUT action, the student\u2019s name is going to be updated:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48718\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected.png\" alt=\"Update actions connected\" width=\"1141\" height=\"170\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected.png 1141w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected-300x45.png 300w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected-768x114.png 768w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected-1024x153.png 1024w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/36-Update-actions-connected-1080x161.png 1080w\" sizes=\"auto, (max-width: 1141px) 100vw, 1141px\" \/><\/a><\/p>\n<p>We can see that only the Name property has been updated. If you can&#8217;t see both commands in the console window, you can find them in the Output window inside Visual Studio for sure.<\/p>\n<h3>IsModified Property<\/h3>\n<p>When we have an entity that is already in the <code>Modified<\/code> state, EF Core uses another property that provides information about what really changed. This is the <code>IsModified<\/code> property and we can examine how it works by slightly modifying our code:<\/p>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-highlight=\"11-13\" data-enlighter-title=\"\">[HttpPut(&quot;{id}&quot;)]\r\npublic IActionResult PUT (Guid id, [FromBody] Student student)\r\n{\r\n    var dbStudent = _context.Students\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    dbStudent.Age = student.Age;\r\n    dbStudent.Name = student.Name;\r\n    dbStudent.IsRegularStudent = student.IsRegularStudent;\r\n\r\n    var isAgeModified = _context.Entry(dbStudent).Property(&quot;Age&quot;).IsModified;\r\n    var isNameModified = _context.Entry(dbStudent).Property(&quot;Name&quot;).IsModified;\r\n    var isIsRegularStudentModified = _context.Entry(dbStudent).Property(&quot;IsRegularStudent&quot;).IsModified;\r\n\r\n    _context.SaveChanges();\r\n\r\n    return NoContent()\r\n}\r\n<\/pre><\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/37-IsModified-property.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48719\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/37-IsModified-property.png\" alt=\"IsModified property\" width=\"689\" height=\"129\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/37-IsModified-property.png 689w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/37-IsModified-property-300x56.png 300w\" sizes=\"auto, (max-width: 689px) 100vw, 689px\" \/><\/a><\/p>\n<p>The result is pretty self-explanatory here.<\/p>\n<h2 id=\"updatingrelationships\"><span lang=\"EN-US\">Updating Relationships in EF Core<\/span><\/h2>\n<p>Adding relationships to the update operations in EF Core is pretty easy. We can attach a relational entity to the main entity, modify it and EF Core will do the rest for us as soon as we call the <code>SaveChanges<\/code> method. The procedure is the same as we did it for the create actions.<\/p>\n<p>Let\u2019s see how to update the relationship in EF Core:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPut(\"{id}\/relationship\")]\r\npublic IActionResult UpdateRelationship(Guid id, [FromBody] Student student)\r\n{\r\n    var dbStudent = _context.Students\r\n        .Include(s =&gt; s.StudentDetails)\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    dbStudent.Age = student.Age;\r\n    dbStudent.Name = student.Name;\r\n    dbStudent.IsRegularStudent = student.IsRegularStudent;\r\n    dbStudent.StudentDetails.AdditionalInformation = \"Additional information updated\";\r\n\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>When we send our request form Postman we are going to update the <code>student<\/code> entity and the <code>studentDetails<\/code> entity:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/39-Update-relationships-sql-result.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48721\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/39-Update-relationships-sql-result.png\" alt=\"Update relationships sql result\" width=\"748\" height=\"251\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/39-Update-relationships-sql-result.png 748w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/39-Update-relationships-sql-result-300x101.png 300w\" sizes=\"auto, (max-width: 748px) 100vw, 748px\" \/><\/a><\/p>\n<p>The same process applies to other relationship types.<\/p>\n<p>The important thing to know is when we update our main entity by adding a new relationship entity, EF Core is going to create a new row in the relationship table and connects it to the main entity with a foreign key:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPut(\"{id}\/relationship\")]\r\npublic IActionResult UpdateRelationhip(Guid id, [FromBody] Student student)\r\n{\r\n    var dbStudent = _context.Students\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    dbStudent.StudentDetails = new StudentDetails\r\n    {\r\n        Id = new Guid(\"e2a3c45d-d19a-4603-b983-7f63e2b86f14\"),\r\n        Address = \"added address\",\r\n        AdditionalInformation = \"Additional information for student 2\"\r\n    };\r\n\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/40-Update-relationships-sql-result-for-insert.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48722\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/40-Update-relationships-sql-result-for-insert.png\" alt=\"Update relationships sql result for insert\" width=\"769\" height=\"236\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/40-Update-relationships-sql-result-for-insert.png 769w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/40-Update-relationships-sql-result-for-insert-300x92.png 300w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/40-Update-relationships-sql-result-for-insert-768x236.png 768w\" sizes=\"auto, (max-width: 769px) 100vw, 769px\" \/><\/a><\/p>\n<p>Since now we know how to update relationships in EF Core, we can continue on to the disconnected update.<\/p>\n<h2 id=\"disconnectedupdate\"><span lang=\"EN-US\">Disconnected Update in EF Core<\/span><\/h2>\n<p>There are several ways to execute a disconnected update, and we are going to show you two ways of doing that. In the first example, we are going to attach the object sent from a client, modify its state and then save it to the database:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPut(\"disconnected\")]\r\npublic IActionResult UpdateDisconnected([FromBody] Student student)\r\n{\r\n    _context.Students.Attach(student);\r\n    _context.Entry(student).State = EntityState.Modified;\r\n\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>The <code>student<\/code> object sent from the client has a <code>Detached<\/code> state at a beginning. After we use the <code>Attach<\/code> method the object is going to change state to <code>Unchanged<\/code>.<\/p>\n<p>This also means that as of this moment, EF Core starts tracking the entity. Now, we are going to change the state to <code>Modified<\/code> and save it to the database.<\/p>\n<p>This is the object sent from the client:<\/p>\n<p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-title=\"\">{\r\n    &quot;id&quot;: &quot;DF75D335-A0EA-4798-8D8E-D7BF940F4D1D&quot;,\r\n    &quot;name&quot;: &quot;Student updated 3&quot;,\r\n    &quot;age&quot;: &quot;22&quot;,\r\n    &quot;isRegularStudent&quot;: false\r\n}\r\n<\/pre><\/p>\n<p>And as you can see it has the <code>Id<\/code> property as well. Additionally, we have changed the <code>Name<\/code> and <code>IsRegularStudent<\/code> properties, but EF Core will update the entire object in the database.<\/p>\n<p>Another way of doing the same thing is by using the <code>Update<\/code> or <code>UpdateRange<\/code> method if we have multiple objects ready for an update. So let\u2019s send the same object with only the <code>IsRegularStudent<\/code> property modified to true:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpPut(\"disconnected\")]\r\npublic IActionResult UpdateDisconnected([FromBody] Student student)\r\n{\r\n    _context.Update(student);\r\n\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>We can see the difference. The <code>Update<\/code> method will set the entity to tracked and also modify its state from <code>Detached<\/code> to <code>Modified<\/code>. So, we don\u2019t have to attach the entity and to modify its state explicitly because the <code>Update<\/code> method does that for us. This approach is also going to update the entire object even though we changed just one property:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/41-Disconnected-update-command.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48723\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/41-Disconnected-update-command.png\" alt=\"Disconnected update command\" width=\"688\" height=\"81\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/41-Disconnected-update-command.png 688w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/41-Disconnected-update-command-300x35.png 300w\" sizes=\"auto, (max-width: 688px) 100vw, 688px\" \/><\/a><\/p>\n<p>There we go.<\/p>\n<p>Now we know how to execute a disconnected update and what is the difference from a connected update.<\/p>\n<h2 id=\"deletingoperations\"><span lang=\"EN-US\">Delete Operations in EF Core<\/span><\/h2>\n<p>The last way to modify data is by using delete actions. We have two ways of deleting rows and we are going to cover them both.<\/p>\n<p>So, let\u2019s start with the Soft Delete approach.<\/p>\n<h3><span lang=\"EN-US\">Soft Delete in Entity Framework Core<\/span><\/h3>\n<p>With the Soft Delete approach, the entity is hidden rather than deleted. This is not an unusual way of doing delete actions because in many real-world projects we don\u2019t want to really delete any row because they will be required for some statistics later on.<\/p>\n<p>So basically, we are not deleting an entity we are updating it by modifying its property (we are going to name it Deleted) to true.<\/p>\n<p>To show how soft delete works, we need to make some changes in our current database model. To start, let\u2019s modify the <code>Student<\/code> model by adding additional property:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public bool Deleted { get; set; }<\/pre>\n<p>Now, let\u2019s modify the <code>StudentConfiguration<\/code> class, by adding an additional method to filter out all the Deleted students from the queries:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">builder.HasQueryFilter(s =&gt; !s.Deleted);<\/pre>\n<p>The <code>HasQueryFilter<\/code> method will include only those <code>Student<\/code> entities with the <code>Deleted<\/code> property set to false. Or in other words, it will ignore all the students with Deleted set to true.<\/p>\n<p>To apply these changes to the database, we are going to create and apply migration:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">PM&gt; Add-Migration AddedDeletedPropertyToStudent\r\nPM&gt; Update-Database\r\n<\/pre>\n<p>After this migration, all the rows in the <code>Student<\/code> table will have the <code>Deleted<\/code> column set to zero (false).<\/p>\n<p>To continue on, let\u2019s create additional Delete action in the controller and send the Delete request:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpDelete(\"{id}\")]\r\npublic IActionResult Delete(Guid id)\r\n{\r\n    var student = _context.Students\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    if (student == null)\r\n        return BadRequest();\r\n\r\n    student.Deleted = true;\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>The soft delete consists of three parts:<\/p>\n<ul>\n<li>Loading part<\/li>\n<li>Modifying part<\/li>\n<li>Saving part<\/li>\n<\/ul>\n<p>This is the result in the database:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/42-Soft-delete-sql-result.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-48724 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/42-Soft-delete-sql-result.png\" alt=\"Soft delete sql result - Modifying data\" width=\"576\" height=\"162\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/42-Soft-delete-sql-result.png 576w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/42-Soft-delete-sql-result-300x84.png 300w\" sizes=\"auto, (max-width: 576px) 100vw, 576px\" \/><\/a><\/p>\n<h3><span lang=\"EN-US\">Loading Data with QueryFilter implemented and Ignoring QueryFilter<\/span><\/h3>\n<p>With the <code>HasQueryFilter<\/code> method we\u2019ve made sure that \u201cdeleted\u201d entities won\u2019t be included in the query result. Let\u2019s see if it works:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var studentsWithoudDeleted = _context.Students.ToList();<\/pre>\n<p>EF Core would usually return all the students from the Student table. But with the <code>HasQueryFilter<\/code> method implemented, the result is different:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/43-HasQueryFilterImplemented.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48725\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/43-HasQueryFilterImplemented.png\" alt=\"HasQueryFilterImplemented\" width=\"557\" height=\"135\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/43-HasQueryFilterImplemented.png 557w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/43-HasQueryFilterImplemented-300x73.png 300w\" sizes=\"auto, (max-width: 557px) 100vw, 557px\" \/><\/a><\/p>\n<p>We can confirm that the \u201cStudent updated 3\u201d is missing, which is the only one with the Deleted property set to true.<\/p>\n<p>Of course, if we want to ignore our query filter we can do that by applying the <code>IgnoreQueryFilter<\/code> to the query:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var studentsWithDeleted = _context.Students\r\n    .IgnoreQueryFilters()\r\n    .ToList();\r\n<\/pre>\n<p>This query will include the \u201cdeleted\u201d entity:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/44-IgnoreQueryFIlter-Implemented.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-48726 size-full\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/44-IgnoreQueryFIlter-Implemented.png\" alt=\"IgnoreQueryFIlter Implemented - Modifying data\" width=\"548\" height=\"153\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/44-IgnoreQueryFIlter-Implemented.png 548w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/44-IgnoreQueryFIlter-Implemented-300x84.png 300w\" sizes=\"auto, (max-width: 548px) 100vw, 548px\" \/><\/a><\/p>\n<p>Great job. Let\u2019s continue with the regular Delete.<\/p>\n<h3><span lang=\"EN-US\">Delete a Single Entity with EF Core<\/span><\/h3>\n<p>In the regular delete, we are not modifying our entity but actually removing it from the database by using the <code>Remove<\/code> method or <code>RemoveRange<\/code> method for multiple entities:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpDelete(\"{id}\")]\r\npublic IActionResult Delete(Guid id)\r\n{\r\n    var student = _context.Students\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    if (student == null)\r\n        return BadRequest();\r\n\r\n    _context.Remove(student);\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>There are three steps for this action as well:<\/p>\n<ul>\n<li>Loading the student entity and set its state to Unchanged<\/li>\n<li>Using the Remove method to set the state to Deleted<\/li>\n<li>Saving the changes in the database<\/li>\n<\/ul>\n<p>We must be careful with Delete actions if our entity has relationships and we haven\u2019t specified them in the delete action. Deleting the main entity could delete the relationships as well (cascade delete), depending on the entity configuration.<\/p>\n<p>We have talked about configuring delete actions in the <a href=\"https:\/\/code-maze.com\/efcore-relationships\/\" target=\"_blank\" rel=\"noopener noreferrer\">Entity Framework Core Relationships<\/a> article.<\/p>\n<p>This is the translated SQL command:<\/p>\n<p><a href=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/45-Delete-Single-Entity.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-48727\" src=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/45-Delete-Single-Entity.png\" alt=\"Delete Single Entity\" width=\"961\" height=\"184\" srcset=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/45-Delete-Single-Entity.png 961w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/45-Delete-Single-Entity-300x57.png 300w, https:\/\/code-maze.com\/wp-content\/uploads\/2019\/08\/45-Delete-Single-Entity-768x147.png 768w\" sizes=\"auto, (max-width: 961px) 100vw, 961px\" \/><\/a><\/p>\n<p>We can see that the first Select statement includes the query filter. Right after the filter there is our Delete query.<\/p>\n<h3><span lang=\"EN-US\">Delete Entity with Relationships<\/span><\/h3>\n<p>To delete an entity with its relationships, all we have to do is to include that relationship into the main entity:<\/p>\n<pre  class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[HttpDelete(\"{id}\/relationship\")]\r\npublic IActionResult DeleteRelationships(Guid id)\r\n{\r\n    var student = _context.Students\r\n        .Include(s =&gt; s.StudentDetails)\r\n        .FirstOrDefault(s =&gt; s.Id.Equals(id));\r\n\r\n    if (student == null)\r\n        return BadRequest();\r\n\r\n    _context.Remove(student);\r\n    _context.SaveChanges();\r\n\r\n    return NoContent();\r\n}\r\n<\/pre>\n<p>And that is all we have to do. EF Core will do the rest for us.<\/p>\n<h2><span lang=\"EN-US\">Conclusion<\/span><\/h2>\n<p>Excellent. We have covered a lot of topics related to Entity Framework Core and its usage in the ASP.NET Core.<\/p>\n<p>In this series, we\u2019ve learned how to integrate EF Core in the ASP.NET Core app and how to set up non-relational and relational configuration. Furthermore, we have learned to use Migrations, Queries, and Database modification operations.<\/p>\n<p>We hope that you have enjoyed reading it and that you have found a lot of useful information.<\/p>\n<p>Until the next series\u2026<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Until now, we have talked about the EF Core integration into the ASP.NET Core app, how to configure nonrelational properties and how to create relationships in EF Core. Additionally, we talked about migrations and the way to use queries to fetch the data from the database. So now, to wrap up this series, we are [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":54839,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[379,570,575,573,245,571,574,572],"class_list":["post-48711","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-add","tag-addrange","tag-change-tracker","tag-disconnected-update","tag-remove","tag-removerange","tag-sate-ef-core","tag-update","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Modifying Data with Entity Famework Core - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we are going to talk about modifying data with EF Core by using differnet approaches and different actions.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/efcore-modifying-data\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Modifying Data with Entity Famework Core - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to talk about modifying data with EF Core by using differnet approaches and different actions.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/efcore-modifying-data\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2019-09-02T06:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-01-13T09:52:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Marinko Spasojevi\u0107\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Marinko Spasojevi\u0107\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"16 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/\"},\"author\":{\"name\":\"Marinko Spasojevi\u0107\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533\"},\"headline\":\"Modifying Data with Entity Famework Core\",\"datePublished\":\"2019-09-02T06:00:52+00:00\",\"dateModified\":\"2022-01-13T09:52:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/\"},\"wordCount\":2393,\"commentCount\":14,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png\",\"keywords\":[\"Add\",\"AddRange\",\"Change Tracker\",\"Disconnected Update\",\"Remove\",\"RemoveRange\",\"Sate Ef Core\",\"Update\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/efcore-modifying-data\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/\",\"url\":\"https:\/\/code-maze.com\/efcore-modifying-data\/\",\"name\":\"Modifying Data with Entity Famework Core - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png\",\"datePublished\":\"2019-09-02T06:00:52+00:00\",\"dateModified\":\"2022-01-13T09:52:16+00:00\",\"description\":\"In this article, we are going to talk about modifying data with EF Core by using differnet approaches and different actions.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/efcore-modifying-data\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png\",\"width\":1100,\"height\":620,\"caption\":\"06 - ef core series modifying data\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/efcore-modifying-data\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Modifying Data with Entity Famework Core\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533\",\"name\":\"Marinko Spasojevi\u0107\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg\",\"caption\":\"Marinko Spasojevi\u0107\"},\"description\":\"Hi, my name is Marinko Spasojevic. Currently, I work as a full-time .NET developer and my passion is web application development. Just getting something to work is not enough for me. To make it just how I like it, it must be readable, reusable, and easy to maintain. Prior to being an author on the CodeMaze blog, I had been working as a professor of Computer Science for several years. So, sharing knowledge while working as a full-time developer comes naturally to me.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/marinko-spasojevic\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/marinko\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Modifying Data with Entity Famework Core - Code Maze","description":"In this article, we are going to talk about modifying data with EF Core by using differnet approaches and different actions.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/efcore-modifying-data\/","og_locale":"en_US","og_type":"article","og_title":"Modifying Data with Entity Famework Core - Code Maze","og_description":"In this article, we are going to talk about modifying data with EF Core by using differnet approaches and different actions.","og_url":"https:\/\/code-maze.com\/efcore-modifying-data\/","og_site_name":"Code Maze","article_published_time":"2019-09-02T06:00:52+00:00","article_modified_time":"2022-01-13T09:52:16+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png","type":"image\/png"}],"author":"Marinko Spasojevi\u0107","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Marinko Spasojevi\u0107","Est. reading time":"16 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/"},"author":{"name":"Marinko Spasojevi\u0107","@id":"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533"},"headline":"Modifying Data with Entity Famework Core","datePublished":"2019-09-02T06:00:52+00:00","dateModified":"2022-01-13T09:52:16+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/"},"wordCount":2393,"commentCount":14,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png","keywords":["Add","AddRange","Change Tracker","Disconnected Update","Remove","RemoveRange","Sate Ef Core","Update"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/efcore-modifying-data\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/efcore-modifying-data\/","url":"https:\/\/code-maze.com\/efcore-modifying-data\/","name":"Modifying Data with Entity Famework Core - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png","datePublished":"2019-09-02T06:00:52+00:00","dateModified":"2022-01-13T09:52:16+00:00","description":"In this article, we are going to talk about modifying data with EF Core by using differnet approaches and different actions.","breadcrumb":{"@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/efcore-modifying-data\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2019\/09\/06-ef-core-series-modifying-data.png","width":1100,"height":620,"caption":"06 - ef core series modifying data"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/efcore-modifying-data\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"Modifying Data with Entity Famework Core"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/d6fa06e66820968d19b39fb63cff2533","name":"Marinko Spasojevi\u0107","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/marinko-1x1-3-150x150.jpg","caption":"Marinko Spasojevi\u0107"},"description":"Hi, my name is Marinko Spasojevic. Currently, I work as a full-time .NET developer and my passion is web application development. Just getting something to work is not enough for me. To make it just how I like it, it must be readable, reusable, and easy to maintain. Prior to being an author on the CodeMaze blog, I had been working as a professor of Computer Science for several years. So, sharing knowledge while working as a full-time developer comes naturally to me.","sameAs":["https:\/\/www.linkedin.com\/in\/marinko-spasojevic\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/marinko\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/48711","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=48711"}],"version-history":[{"count":2,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/48711\/revisions"}],"predecessor-version":[{"id":63339,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/48711\/revisions\/63339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/54839"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=48711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=48711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=48711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}