Open Closed

Strange behavior while inserting the entity by using InsertMany #7299


User avatar
0
uyarbtrlp created
  • ABP Framework version: v8.1.3
  • UI Type: MVC
  • Database System: EF Core (SQL Server, Oracle, MySQL, PostgreSQL, etc..)
  • Module project (Separated Deployment & Databases Scenario): yes

Hello,

I have an aggregate root - value object relationship on my module. As you can see there are profile aggregate root and edge definition value object Relationship has been described in EFCore like this. I use owns many relationship between Profile and EdgeDefinition. I have a method that creates multiple profiles according to input that is sent in ProfileAppService. When I create two profiles, two entitites, I see that only one edge information on my local db and it is assigned to latest profile. So I examine the EF core logs it seems that InsertMany create a single edge definition and updates the FK with the latest one. It is a really strange behavior because I expect that it should create single value object for each profile. To give an example. I sent two ids for profile and it creates two profiles, but I can only see the latest one on edge definition table. Since it is a long log file I could not send it. But you can see that it changes the ProfileId value while inserting it. 2024-06-05 12:03:44.113 +03:00 [DBG] The foreign key property 'EdgeDefinition.ProfileId' was detected as changed from '"dd9c0686-7486-c122-0940-3a12f9cb4247"' to '"273b1940-0173-8d1d-3bc0-3a12f9d07589"' for entity with key '{Id: 372}'. I just want to see that if it works InsertAsync method with autosave but there is same issue. It seems it could not save the transaction. The only thing works when we create a new unit of work. It basically creates edge definition for each profile. It seems it is a really strange problem even though we want to save it with autosave. I just want to use InsertMany method but it does not work. Do you have any idea why it happens?


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

    Could you use the CLI to create a new project to reproduce the problem and share it with me? I will check it. My email is shiwei.liang@volsoft.com

    thanks

  • User Avatar
    0
    uyarbtrlp created

    I've sent it

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I could not reproduce the problem

  • User Avatar
    0
    uyarbtrlp created

    It should create 3 edge definitions for each profile. You should see 9 different edge definitions. I can only see the edge definitions which are belong to one profile which is starting with Guid “BCF”. If you try the working code, you see the difference

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I think this is how the value object works : https://docs.abp.io/en/abp/latest/Value-Objects

    Two Entities with the same properties but with different Ids are considered as different entities. However, Value Objects have no Ids and they are considered as equals if they have the same property values.

  • User Avatar
    0
    uyarbtrlp created

    I just want to add aggregate root-value object relation as batch. So you can see that inside the profile list, I have multiple profiles and its edge definitions. I expect that when I send the request, it should create 3 edge definitions record for each profile (total 9 edge definition records), because I have the FK-PK relationship between Profile and EdgeDefinition with OwnsMany relation which can be used for value objects. In logs, somehow it changes the foreign key property to latest one, but I expect that it should create different edge definition object not changing the FK. And also, when I try to save it immediately with InsertAsync method, I got an exception It seems to me there is a transactional issue. I expect that it should create one profile and 3 edge definitions each loop. Should we expect that value object can be changed like that while sending the aggregate root as batch? I expect that it should create aggregate - value object relation without changing anything.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    HI,

    As I said, this is how the value object works, Value objects with the same property value are considered the same object.

    You should consider use Entity instead of Value object

  • User Avatar
    0
    uyarbtrlp created

    Okay, I have another question. Why it is working when I create a new unit of work scope?

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    HI,

    Because it is a new independent transaction, EF Core entity tracking doesn't detect them

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