Finbuckle.MultiTenant icon indicating copy to clipboard operation
Finbuckle.MultiTenant copied to clipboard

Error when saving an entity with a populated collection in a 1-to-many EF Core relationship: Tenant ID not set in Finbuckle.MultiTenant

Open JonasDev17 opened this issue 1 year ago • 9 comments

When adding an entity to a collection property of another entity (representing a 1-to-many relationship mapped by EF Core) and attempting to save changes, the following error occurs:

Finbuckle.MultiTenant.MultiTenantException : 1 modified entities with Tenant Id not set.

Is this expected? This used to work but something changed (in my code) but I have no clue what it could be. The workaround is to add it through the DbContext.

Is there some condition for this to work?

This is the method I am calling on my entity:

public Device AddNetworkInterfaceController(Guid ipAddressId, string? macAddress, string? name, string? description)
{
    var deviceNetowrkCard = new NetworkInterfaceController(this.Id, ipAddressId, macAddress, name, description);
    NetworkInterfaceControllers.Add(deviceNetowrkCard);
    return this;
}

and after that I call SaveChangesAsync. When debugging EnforceMultiTenant I can see that it is this NIC entity which is in the notSetModified List.

JonasDev17 avatar Nov 26 '24 11:11 JonasDev17

Actually this is using an old version of Finbuckle.MultiTenant. I haven't gotten around to upgrading due to breaking changes. Still weird that this would suddenly break...

JonasDev17 avatar Nov 26 '24 15:11 JonasDev17

Hi, I think it should be as simple as setting TenantNotSetMode to overwrite on your db context.

AndrewTriesToCode avatar Nov 26 '24 16:11 AndrewTriesToCode

Hi, I think it should be as simple as setting TenantNotSetMode to overwrite on your db context.

I am just a little hesitant doing this knowing it used to work without that setting. Or is this a known requirement for my use case and it just silently failed on me previously?

JonasDev17 avatar Nov 26 '24 17:11 JonasDev17

FYI

I had the same issue "1 modified entities with Tenant Id not set." Although I was just updating an entity, I didn't delete anything... (at least not in the method where this error was throwing).

After some debugging, I found a bug in my code, where I deleted another entity twice (in the same dbcontext in an unrelated event handler). Apparently that's no problem for efcore, which simply doesn't do anything when you try to delete an entity the second time, but I think that's a case that's not handled correctly by Finbuckle...

When I fixed the bug, so the delete was only done once, everything worked again.

I also think this one is somehow related: https://github.com/Finbuckle/Finbuckle.MultiTenant/discussions/651

fretje avatar Dec 17 '24 11:12 fretje

@fretje

Turns out it was something very strange... Fullstackhero uses a BaseEntity and AuditableEntity:

public abstract class BaseEntity : BaseEntity<Guid>
{
    protected BaseEntity() => Id = RT.Comb.Provider.PostgreSql.Create();
}

I removed the BaseEntity<Guid> and made the BaseEntity implement IEntity<Guid> and same for the AuditableEntity<Guid> and this somehow caused this problem... I have no clue why but it wasn't an important change so I reverted the changes...

JonasDev17 avatar Dec 17 '24 15:12 JonasDev17

Thanks for the update. I'm glad you got it working for you but I would like to understand what was going on so let me know if you see anything else. Finbuckle is just looking at the db context's tracked changes and if it sees any that are missing the tenant it optionally throws that error. The only thing I can think of is if a delete is cascading or some other code is adjusting tracking entries outside of your own.

AndrewTriesToCode avatar Dec 18 '24 16:12 AndrewTriesToCode

This issue has been labeled inactive because it has been open 30 days with no activity. This will be closed in 7 days without further activity.

github-actions[bot] avatar Mar 21 '25 02:03 github-actions[bot]

edit: No this will not be closed--sorry about that!

AndrewTriesToCode avatar Mar 21 '25 05:03 AndrewTriesToCode

This issue has been labeled inactive because it has been open 180 days with no activity. Please consider closing this issue if no further action is needed.

github-actions[bot] avatar Sep 22 '25 02:09 github-actions[bot]