C
C#4w ago
Scipio

'FOREIGN KEY constraint failed in Sqlite using Entity Framework Core and WinUi 3

I am getting the following error when attempting to add to add a record to my SQLite database: SqliteException: SQLite Error 19: 'FOREIGN KEY constraint failed' The exception occurs in ClientViewModel.cs - line 74 Link to my project: https://github.com/SpaceWarlord/Roster
GitHub
GitHub - SpaceWarlord/Roster
Contribute to SpaceWarlord/Roster development by creating an account on GitHub.
26 Replies
Jimmacle
Jimmacle4w ago
can you share the full exception? it should tell you which constraint caused the error but generally that means you tried to save a relationship that's not valid that's what foreign keys do, they relate rows in one table to rows in another table which means they always have to be a valid key in that table (or null in some cases) so chances are you're adding an item with a foreign key that points to no valid row, or deleting an item that is referenced by a foreign key somewhere else
Scipio
ScipioOP3w ago
No description
Jimmacle
Jimmacle3w ago
i also really don't recommend coupling your interface code and your database code as tightly as you are a synchronous SaveChanges is going to hang your UI and your entities shouldn't implement INPC
Scipio
ScipioOP3w ago
inpc?
Jimmacle
Jimmacle3w ago
INotifyPropertyChanged
Scipio
ScipioOP3w ago
you mean my models?
Jimmacle
Jimmacle3w ago
your database models, yes also, dbcontexts should not be long lived like they are in your code they should be created, used for an operation, then disposed
Scipio
ScipioOP3w ago
a guy the other day in the #gui chat said the opposite that you should use one rather then multiple
Jimmacle
Jimmacle3w ago
that is very bad advice they're designed to be short-lived for example, you can't run 2 queries at once on the same dbcontext and the change tracker will get stale and full of junk the longer you keep it which can lead to weird issues with the state of your database
Scipio
ScipioOP3w ago
ok didnt know that how would i go about decoupling my code more i thought that's what i was doing with all this mvvm stuff (im a winforms guy moving over to winui ) at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db) at Microsoft.Data.Sqlite.SqliteDataReader.NextResult() at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior) at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader() at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(IRelationalConnection connection) thats the stack trace for the inner exception
Natashi
Natashi3w ago
Try using this package, it'll give you more details on db exceptions https://www.nuget.org/packages/EntityFrameworkCore.Exceptions.Common
EntityFrameworkCore.Exceptions.Common 8.1.3
Handle database errors easily when working with Entity Framework Core. Catch specific exceptions such as UniqueConstraintException, CannotInsertNullException, MaxLengthExceededException, NumericOverflowException or ReferenceConstraintException instead of generic DbUpdateException This is a base package for database specific packages. Install on...
Jimmacle
Jimmacle3w ago
use different classes for your DB models and your MVVM models and map between them and an easy solution for the dbcontext is to switch to IDbContextFactory and create new dbcontexts through that when you actually need them
Scipio
ScipioOP3w ago
No description
Scipio
ScipioOP3w ago
With that package installed its now saying I have reference constraint exception Do you have any examples or tutorials on how to do this for a beginner?
Natashi
Natashi3w ago
Expand the exception, see the property ConstraintName
Scipio
ScipioOP3w ago
No description
Scipio
ScipioOP3w ago
its null for some reason
Natashi
Natashi3w ago
Well that's not good Are you sure you followed all the steps in the package's readme
Scipio
ScipioOP3w ago
i'll double check optionsBuilder.UseExceptionProcessor();
optionsBuilder.UseSqlite("Data Source=database27.db"); optionsBuilder.EnableSensitiveDataLogging(true); thats what i have in my OnConfiguring which is what the guide says to do
Scipio
ScipioOP3w ago
No description
Scipio
ScipioOP3w ago
"[!WARNING] ConstraintName and ConstraintProperties will not be populated when using SQLite." ok nvm its cause im using sqlite
Jimmacle
Jimmacle3w ago
it's just OOP stuff, like var uiModel = new MyUiModel(myEntity); with code that takes all the data from the entity and puts it in the ui model
Scipio
ScipioOP3w ago
ok and whats the benefits of doing it that way
Jimmacle
Jimmacle3w ago
it separates the code that is concerned with working with the UI and the code that is concerned with modeling your database so if you have to change one it's easier to avoid breaking the other
Scipio
ScipioOP3w ago
ok that makes sense @Jimmacle hi i am bit confused with implementing this entity to model thing within my model class should i store a reference to the entity? something like this: public class UserModel(){ UserEntity userEntity; public UserModel(string id, string name, ){ userEntity=new UserEntity(id, name); } }
Scipio
ScipioOP3w ago
GitHub
GitHub - SpaceWarlord/Roster
Contribute to SpaceWarlord/Roster development by creating an account on GitHub.
Want results from more Discord servers?
Add your server