Base solution for your next web application
Open Closed

_repository.Update updates all columns #4173


User avatar
0
mamak created

Hi there, I have an simple entity like that

public class AnnouncementEntity { public int Id { get; set;} public string Header {get; set;} public string Content {get; set;} }

In my application service i have an update method: public async Task UpdateAnnouncement(EditAnnouncementInputDto input) {

        AnnouncementEntity announce  = new AnnouncementEntity()
        {
            Id = input.Id,
            Header = input.Header
        };

        await _announceRepository.UpdateAsync(announce);
    }

this method updates the entity but i have a Content column and this update makes Content NULL.

How can i update columns that i want not all columns?


10 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    To do that, you should attach the entity.

    // Create new stub with correct id and attach to context.
    var announce = new AnnouncementEntity { Id = input.Id };
    repository.As<EfCoreRepositoryBase<MyDbContext, AnnouncementEntity>>().Table.Attach(announce);
    
    // Now the entity is being tracked by EF, update required properties.
    announce.Header = input.Header;
    
    // EF knows only to update the properties specified above.
    _unitOfWorkManager.Current.SaveChanges();
    
  • User Avatar
    0
    mamak created

    Thank you for your reply.

    I understand that i should attach the entity to the DbContext.

    But i cant figure it out how.

    Could you be more specific with this line:

    repository.As<EfCoreRepositoryBase<MyDbContext, AnnouncementEntity>>().Table.Attach(announce);

    Btw im not using EfCore.

    my _announcementRepo doesnt have "As" method.

  • User Avatar
    0
    aaron created
    Support Team

    Could you be more specific with this line:

    repository.As<EfCoreRepositoryBase<MyDbContext, AnnouncementEntity>>().Table.Attach(announce);

    Btw im not using EfCore.

    Please specify that in future questions.

    And simply remove Core.

    my _announcementRepo doesnt have "As" method.

    Add this using directive:

    using Abp.Extensions;
    

    You can hold Ctrl + . to fix such reference issues.

  • User Avatar
    0
    mamak created

    Thank you for your reply and patience (:

    I solved reference and using problems. But when code comes to this line

    repository.As<EfCoreRepositoryBase<MyDbContext, AnnouncementEntity>>().Table.Attach(announce);

    Unable to cast object of type 'Castle.Proxies.IRepository1Proxy_4' to type 'Abp.EntityFramework.Repositories.EfRepositoryBase2[ProjectName.EntityFramework.MyDbContext,AnnouncementEntity]'

  • User Avatar
    0
    aaron created
    Support Team

    Is your DbContext actually called MyDbContext?

  • User Avatar
    0
    mamak created

    Ofc not :)

  • User Avatar
    0
    mamak created

    This is the part of my Update service method.

    public async Task UpdateAnnouncement(EditAnnouncementInputDto input) { var announce = input.MapTo<AnnounceEntity>();

            _announceRepository.As&lt;EfRepositoryBase&lt;MyProjectDbContext, AnnouncementEntity&gt;>().Table.Attach(announce);
    
             await _duyuruRepository.UpdateAsync(announce);
       }
    
  • User Avatar
    0
    aaron created
    Support Team

    Can you put a breakpoint and check the actual type of _announceRepository?

  • User Avatar
    0
    mamak created

    Its type is "IRepository<AnnounceEntity>"

  • User Avatar
    0
    aaron created
    Support Team

    Try this:

    ProxyHelper.UnProxy(_announceRepository).As<EfRepositoryBase<MyProjectDbContext, AnnouncementEntity>>().Table.Attach(announce);
    

    Add this using directive:

    using Abp.Reflection;