Open Closed

AbpDbContext issue with while setting IDs for child entities #2416


User avatar
0
rafal.woznicki created
  • ABP Framework version: v5.0.0
  • UI type: Angular
  • DB provider: EF Core

Hi,

I've noticed some issue while adding child entities. Entity:

Entity configuration:

Issue:

By default AbpDbContext sets ID for added entity and it works properly when you add one child and call Update on repository, but when you add two children and call Update on repo then exception is throwed (as on the picture) Is it a framework issue? or maybe my implementation is wrong?

What is the workaround? Generating IDs by my own?

Regards


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

    Hi,

    Will this problem occur if you use EF Core directly?

  • User Avatar
    0
    rafal.woznicki created

    Hi,

    what do you mean by "EF Core directly"? Without using abp repository, without using AbpDbContext? If you want me to check whether it works without using AbpDbContext then it makes no sense for me because I want to use abp.io built in feature to automatically generate IDs for atttached (to DBContext) subentities. In my opiniony this feature does not work correctly. What is your opinion?

    I'm talking about method AbpDbContext.TrySetGuidId(...)

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I will check it.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi

    I reproduced the problem.

    You are right, ABP will automatically generate Id after entity tracking but EF Core checks for duplicate Ids and throws an exception when tracking entities.

    I will try to find a way to solve the problem, for now, you need to generate ids for Children.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    It seems you need to set the id of your entity.

    Always set GUID Ids as best practice.

    See https://github.com/abpframework/abp/blob/dev/docs/en/Entities.md#entities-with-guid-keys

    TrySetGuidId

    This method doesn't seem to be compatible with EF Core's tracking functionality, I'll keep checking.

Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09