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

Add TenantNotSetMode.OverwriteIfNullOrEmpty #782

Open
fretje opened this issue Jan 25, 2024 · 4 comments
Open

Add TenantNotSetMode.OverwriteIfNullOrEmpty #782

fretje opened this issue Jan 25, 2024 · 4 comments

Comments

@fretje
Copy link

fretje commented Jan 25, 2024

See our conversation: #612 (reply in thread):

Hi @AndrewTriesToCode.
Maybe I wasn't making myself clear... I don't want a record with a null (or empty) tenantId value... I want Finbuckle to take care of (i.e. set) the tenantId, just like it does with all the other tenantId's in the database... It's just that in this case, the tenantId is part of the primary key, which means I can't use DbContext.Set<T>.Add(entity) with an entity that has that tenantId set to null. You get an EF Core exception when you try to do that. I can add it when the tenantId is emtpy though... but then it doesn't get picked up by Finbuckle when I use TenantNotSetMode, only with TenantMismatchMode...

I guess my question is: could the test you do with TenantNotSetMode use string.IsNullOrEmpty(tenantId) in stead of tenantId is null? ;-)

Originally posted by @fretje in #612 (reply in thread)

hi @fretje

That would be a breaking change, maybe another value for that like TenantNotSetMode.OverwriteIfNullOrEmpty? Would you mind creating a new issue for this?

Originally posted by @AndrewTriesToCode in #612 (reply in thread)

@AndrewTriesToCode
Copy link
Sponsor Contributor

I'm going to look further into this. I'm rethinking some of the EF Core stuff overall so I appreciate your ideas. Have you looked at using generators or similar EFCore capability to set the tenant when using Add?

@fretje
Copy link
Author

fretje commented Feb 28, 2024

Not sure what you mean with "generators"...

I'm using "TenantMismatchMode.Overwrite" for now...

If I don't do that, I have to set the "TenantId" property manually (take it from ITenantInfo) before adding the entity to the dbcontext.

Note that this is only the case for entities where the TenantId is part of the primary key. For other (multitenant) entities (that don't have the TenantId as part of their primary key) I don't even have a "TenantId" property on the entity. There then Finbuckle is taking care of everyting "behind the scenes"... It would be nice if that would also be possible for entities where the TenantId is part of the primary key... But I'm not sure that's even possible with EF Core (i.e. to have an entity where part of its primary key is a shadow property).

@AndrewTriesToCode
Copy link
Sponsor Contributor

AndrewTriesToCode commented Feb 28, 2024

This is what I was referring to:
https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations#primary-keys

Do you think that would help in your use case?

@fretje
Copy link
Author

fretje commented Feb 29, 2024

Oh, you mean database generated id's... well, I can't use those in this case, as here, the "primary key" of the entity (from the tenants perspective) is a user-supplied string, so it needs to be unique, but only per tenant (i.e. 2 separate tenants could use the same primary key here, hence why tenantId needs to be part of the primary key...)

In other cases, I'm indeed using a db generated (identity) id, but in this case that's not possible.

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

No branches or pull requests

2 participants