Error when saving an entity with a populated collection in a 1-to-many EF Core relationship: Tenant ID not set in Finbuckle.MultiTenant
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.
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...
Hi, I think it should be as simple as setting TenantNotSetMode to overwrite on your db context.
Hi, I think it should be as simple as setting
TenantNotSetModeto 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?
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
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...
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.
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.
edit: No this will not be closed--sorry about that!
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.