Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
JonasDev17 opened this issue Nov 26, 2024 · 6 comments
Labels

Comments

@JonasDev17
Copy link

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
Copy link
Author

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...

@AndrewTriesToCode
Copy link
Contributor

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

@JonasDev17
Copy link
Author

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?

@fretje
Copy link

fretje commented Dec 17, 2024

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: #651

@JonasDev17
Copy link
Author

JonasDev17 commented Dec 17, 2024

@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 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...

@AndrewTriesToCode
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants