question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't update record

See original GitHub issue

When trying to update a record for a simple class, I am getting the following error message:

MultiTenantException: 1 modified entities with TenantId not set.

Looking directly at the database, I can see that the TenantID is set just fine when the record was created.

Any ideas why this may be happening?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewTriesToCodecommented, Mar 14, 2019

Hi again, I see the issue.

Because you are using _context.Update(siteSettings) to do a direct update the tenant id is not set on the “untracked” entity because tenant id is a shadow property in EFCore.

You have a few options:

  1. Use TenantNotSetMode.Overwrite which will plug in the tenant Id when saving. Note that this settings will be in effect for any future saves on the same dbcontext in the request.
_context.TenantNotSetMode = TenantNotSetMode.Overwrite;
_context.Update(siteSettings);
await _context.SaveChangesAsync();
  1. You can add a property to the SiteSettings class called TenantId which Finbuckle.MultiTenant will use, then if you make sure it gets passed around in the Get/Post requests the tenant Id should be there when you call Update and SaveChanges.

  2. Avoid using Update and instead get the entity from the database via EFCore then update this “tracked” entity, then save it.

var siteSettings = await _context.SiteSettings.FirstOrDefaultAsync(m => m.Id == id);
siteSettings.Prop1 = model.Prop1;
siteSettings.Prop2 = model.Prop2;
// etc..
await _context.SaveChangesAsync();

I think option 1 is probably the best way to go here.

0reactions
ericsportscommented, Mar 14, 2019

Thanks for the explanation, really appreciate your help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot errors when updating data in query or form
To resolve this problem, you must join the tables correctly so you can update them. When the field that you want to update...
Read more >
Cannot Update Record in PostgreSQL
The UPDATE statement will only work if you have data with matching criteria in the table already. If you are adding records, you...
Read more >
Access 2016 - The form won't allow updating a record
I have an "Edit" button to update individual records and even pressing that the form won't allow me to update any fields on...
Read more >
cannot update record, get stuck - mysql
cannot update record, get stuck · 1. EXPLAIN your query, maybe you have index problems · How can for example an INSERT wait...
Read more >
How To Fix Access Database Not Updating Issue?
To fix this issue, just add a unique index or primary key in the linked table. If you don't have the Update Data...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found