Ends in:
7 DAYS
11 HRS
41 MIN
25 SEC
Ends in:
7 D
11 H
41 M
25 S
Open Closed

ID = 0 when CREATED message is posted to Kafka #5688


User avatar
0
tushar_ww created

Provide us with the following info:

  • ABP Framework version: ABP CLI 7.3.0
  • UI Type: Blazor Server
  • Database System: EF Core (SQL Server)
  • Tiered (for MVC) or Auth Server Separated (for Angular): Yes
  • Exception message and full stack trace: NA
  • Steps to reproduce the issue: Probably applicable

I have enabled ABP framework to post message to Kafka using the builtin mechanism (not PublishAsync()). The message gets posted correctly. But for the CREATED event, the ID = 0. I expected the newly assigned ID to be available in the message. The newly assigned application is available to the code after CreateAsync() call in the AppService. I expected this ID to be available in the message also. Am I missing any configuration? Example:

Let me know what additional information do you need ?


8 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You should manually assign the ID when creating the entity, for example:

    https://docs.abp.io/en/abp/latest/Best-Practices/Entities#example https://github.com/abpframework/abp/blob/dev/modules/identity/src/Volo.Abp.Identity.Application/Volo/Abp/Identity/IdentityUserAppService.cs#L78

  • User Avatar
    0
    tushar_ww created

    Well, I cannot assign the ID manually in the EF (SQL Server) world, because it is being generated in the database by the identity column during insert. You may notice in the message "Id"=0.

    Doesn't ABP framework have any mechanism for database generated int identity-column entities?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    You can consider specifying autosave to create an entity.

    _myRepositpry.InsertAsync(..., autoSave: true)

  • User Avatar
    0
    DNarayanaswamy created

    Hi, I tried this approach it did not work, still Id coming up as 0.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I will check it

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    This is a limitation of EFCore, for the int type, the default value is 0, and ABP can't get the ID value in the entity tracking event.

    I have the following suggestions:

    • Use GUID as best practice instead of int
    • Subscribe to local events instead of distributed events for int primary key type entities. for example:
    public async Task<Book> CreateAsync(string name)
    {
        var book = new Book(name);
    
        return await _bookRepository.InsertAsync(book, autoSave: true);
    }
    
    public class MyHandler : ILocalEventHandler<EntityCreatedEventData<Book>>, ITransientDependency
    {
        public async Task HandleEventAsync(EntityCreatedEventData<Book> eventData)
        {
            var id = eventData.Entity.Id;
        }
    }
    

  • User Avatar
    0
    tushar_ww created

    So switching from ID to GUID is out of question at this point in our application development process.

    InsertAsync() is returning me the ID. So I do not think I need to handle HandleEventAsync() to get the ID.

    If the above statements are correct. then PublishAsync() seems to be the only option for me. And if that is the way to go, then is it enough to remove just this code to stop any default Kafka Publish or do you suggest a different method?

    Configure<AbpDistributedEntityEventOptions>(options => { //Enable for Location entity options.AutoEventSelectors.Add<X>(); options.EtoMappings.Add<X, Y>(); });

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Ok

    If the above statements are correct. then PublishAsync() seems to be the only option for me

    Two options,

    • Subscribe to local events instead of distributed events for int primary key type entities.
    • Publishing events manually via PublishAsync()
Made with ❤️ on ABP v9.1.0-preview. Updated on November 20, 2024, 13:06