Search code examples
asp.net-coreentity-framework-coreaspnetboilerplate

How to disable changestate tracking for a sub-entity in boilerplate AppService


I'm using aspnet core & ef core with boilerplate and would like to disable the changestate tracking for a sub-entity. How do I this this within AppService (ie AsyncCrudAppService).

For example:

Entity:

[Table("tblCategory")]
public class Category : FullAuditedEntity<int>, IMustHaveTenant
{
    public const int MaxCodeLength = 128;
    public const int MaxNameLength = 2048;

    public virtual int TenantId { get; set; }

    [Required]
    [StringLength(MaxCodeLength)]
    public virtual string Code { get; set; }

    [Required]
    [StringLength(MaxNameLength)]
    public virtual string Name { get; set; }

    [ForeignKey("GroupId")]
    public virtual Group Group { get; set; }
    [Required]
    public virtual int GroupId { get; set; }
}

Dto:

[AutoMapFrom(typeof(Category))]
public class CategoryDto : FullAuditedEntityDto<int>
{
    [Required]
    [StringLength(Category.MaxCodeLength)]
    public string Code { get; set; }

    [Required]
    [StringLength(Category.MaxNameLength)]
    public  string Name { get; set; }

    [Required]
    public  int GroupId { get;  set; }

    [DisableValidation]
    public GroupDto Group{ get;  set; }

}

AppService Update Method:

public override async Task<CategoryDto> Update(CategoryDto input)
{
    var cat = await _categoryManager.GetA(input.Id);

    MapToEntity(input, cat);

    //I'd like to disable the tracking of cat.Group here ?

    await _categoryManager.UpdateA(cat);

    return await Get(input);
}

I'd like to disable the change detection for cat.Group, how do I do this ?

Thanks in advance.


Solution

  • Using AsNoTracking when loading resolves the issue