I use UnitOfWork Repositories

Models

public class UTaskModel { public int Id { get; set; } public string Name { get; set; } public DateTime DateCrt { get; set; } public DateTime DateFinish { get; set; } public DateTime DateCncl { get; set; } public string UserCncl { get; set; public string UserCtrl { get; set; } public virtual ICollection<UTaskTargetModel> UTaskTargets { get; set; } } public class UTaskTargetModel { public int Id { get; set; } public int UTaskId { get; set; } public string Text { get; set; } public DateTime DateCmpl { get; set; } } 

The task is planned UTask

I create a record in the database

 public void Create(UTask utask) { db.UTasks.add(utask) } 

through the period, tomorrow, the day after tomorrow, goals are added to UTask

I select by ID UTask and add UTaskTargets

add and update

  public void UpdateUTask(UTaskDTO data) { var item = Database.UTasks.Find(f => f.Id == data.Id).SingleOrDefault(); if (item != null) { AddTargets(data.UTaskTargets); Database.UTasks.Update(Mapper.Map<UTaskDTO, UTaskModel>(data)); Database.Save(); } else { throw new ValidationException("UTask not exist", ""); } } 

UTaskTargets added, after I want to update in UTask DateFinish = DateTime.Now

  public void Update(UTaskModel item) { db.Entry(item).State = System.Data.Entity.EntityState.Modified; } 

Knocks error

  НС ΡƒΠ΄Π°Π»ΠΎΡΡŒ ΠΏΡ€ΠΈΡΠΎΠ΅Π΄ΠΈΠ½ΠΈΡ‚ΡŒ ΡΡƒΡ‰Π½ΠΎΡΡ‚ΡŒ Ρ‚ΠΈΠΏΠ° &quot;NDashApp.DAL.Entities.Feedback.FeedbackModel&quot;, ΠΏΠΎΡΠΊΠΎΠ»ΡŒΠΊΡƒ другая ΡΡƒΡ‰Π½ΠΎΡΡ‚ΡŒ этого ΠΆΠ΅ Ρ‚ΠΈΠΏΠ° ΡƒΠΆΠ΅ ΠΈΠΌΠ΅Π΅Ρ‚ Ρ‚Π°ΠΊΠΎΠ΅ ΠΆΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ΅Ρ€Π²ΠΈΡ‡Π½ΠΎΠ³ΠΎ ΠΊΠ»ΡŽΡ‡Π°. Π’Π°ΠΊΠΎΠ΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€ΠΈ использовании ΠΌΠ΅Ρ‚ΠΎΠ΄Π° &quot;Attach&quot; ΠΈΠ»ΠΈ Π·Π°Π΄Π°Π½ΠΈΠΈ сущности состояния &quot;Unchanged&quot; ΠΈΠ»ΠΈ &quot;Modified&quot;, Ссли ΠΊΠ°ΠΊΠΈΠ΅-Π»ΠΈΠ±ΠΎ сущности Π² Π³Ρ€Π°Ρ„Π΅ ΠΈΠΌΠ΅ΡŽΡ‚ ΠΊΠΎΠ½Ρ„Π»ΠΈΠΊΡ‚ΡƒΡŽΡ‰ΠΈΠ΅ значСния ΠΊΠ»ΡŽΡ‡Π΅ΠΉ. Π’Π°ΠΊΠΎΠ΅ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΠΈΠ·-Π·Π° Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ сущности ΡΠ²Π»ΡΡŽΡ‚ΡΡ Π½ΠΎΠ²Ρ‹ΠΌΠΈ ΠΈ ΠΏΠΎΠΊΠ° Π΅Ρ‰Π΅ Π½Π΅ ΠΏΠΎΠ»ΡƒΡ‡ΠΈΠ»ΠΈ значСния ΠΊΠ»ΡŽΡ‡Π΅ΠΉ, сформированныС Π±Π°Π·ΠΎΠΉ Π΄Π°Π½Π½Ρ‹Ρ…. Π’ этом случаС ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄ &quot;Add&quot; ΠΈΠ»ΠΈ состояниС сущности &quot;Added&quot; для отслСТивания Π³Ρ€Π°Ρ„Π°, Π° Π·Π°Ρ‚Π΅ΠΌ Π·Π°Π΄Π°ΠΉΡ‚Π΅ сущностям, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ Π½Π΅ ΡΠ²Π»ΡΡŽΡ‚ΡΡ Π½ΠΎΠ²Ρ‹ΠΌΠΈ, состояниС &quot;Unchanged&quot; ΠΈΠ»ΠΈ &quot;Modified&quot;. 

I tried a lot, I can not update DateFinish.

How to update DateFinish in UTask?

    1 answer 1

    This is because DbContext tracks the changes of all entities materialized in this session, and if you did not specifically disconnect the entity from the context, then you should not attach it. Just change the desired field and call SaveChanges ().

    Better yet, make all the changes in all the necessary entities, and only then do SaveChanges (). This is precisely the concept of the Entity Framework, it implements the unit of work pattern, you change everything you need and at the end you use everything in one transaction.

    Also, according to your code, I did not see the correct use of DdContext. Just in case an example:

     using(var db = new DbContext()) { // здСсь всС манипуляции db.SaveChanges(); } 

    The point is simple, make as short a living as possible DbContext.

    ps Instead of DbContext, respectively, your class is a successor.

    • Does the lifetime of a DbContext affect performance? - Sergey Tambovtsells
    • The first creation of DbContext warms the model caches, the subsequent ones no longer affect performance. DbContext monitors the status of all requested entities and the longer it is open, the more entities it caches and the more memory it consumes. For the web, it's better to destroy it as quickly as possible, for desktop applications you can leave it open and use it as a cache, but you need to understand what you are doing. For the scenario when you only need to get data from the database, you can use the .AsNoTracking () extension, in this case, materialization will be faster and DbContext will work as a proxy. - Eugene
    • Thanks for the detailed answer!) - Sergey Tambovtsells