Simple AP
Hello im trying to connect c# to database and i have a slight problem with foreign keys here is my code the hierarrchy is firma-> division->project->oddelenie plus there are employes here is my code
60 Replies
also i have this error ntroducing FOREIGN KEY constraint 'FK_DbDiviza_DbZamestnanec_VeduciDivRodneCislo' on table 'DbDiviza' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
its my first time working with this
No zips or word documents please, either upload to github or similar or paste the relevant stuff on $paste
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
however, this is entirely a SQL problem - your database design has errors in it
in this case, your foreign keys have some form of ON DELETE/UPDATE action that conflicts with another foreign key
yes that is wha the error said but i lack the knowledge on how to fix it
check what foreignkeys you have on "DbDiviza"
i have 2
idfirm and id of employe
ok so, if deleting a firm, it makes sense for a division to also be deleted. agreed?
but why is there a foreign key for id of employee?
or do you mean that you have a
employee.DivisionId
field?oh
that makes sens
e
i thought that division would use employe as a division leader but
that seems to get rid of the problem when i
dleted the leaders of divison, project ect
im just gonna add into employe a boolen to set if its a leader of company,div ect
you can have a "division leader" just fine, but it needs to have
ON DELETE NO ACTION
yea but i have no clue how to do that in c# XD
is it like a parameter or a method or
its not related to C# at all
how are you making your database?
im using entity frame work and creating it there
thru migrating
Okay, now we're getting somewhere
What version of EF?
8.0.5
ok good, so modern EF Core
well i did Add-Migration InitialCreate
you'd do this via the EntityConfiguration api, either via a
IEntityTypeConfiguration<T>
or via OnModelConfiguring
and also update-database
lemme check
can you show me your database context source?
preferably via $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/wrong
code
copy the URL from the pastebin 😄
XD
yea
BlazeBin - iseweprgwemw
A tool for sharing your source code with the world!
ok so uhh
not the migration
I need to see the database context
your migration is generated from your context + configurations
so you should delete your existing DatabaseSnapshot + migration
BlazeBin - qdlvfqyrozfp
A tool for sharing your source code with the world!
and re-create it, after we fix your context
ok
ok, no configuration at all
thats your problem
I'll need to see the entities too, thats the classes that are used in your context
ok sec
just a general recommendation, its recommended to keep your code in english
not everyone understands romanian 😛
its slovak but im gonna start using eng i guess
ah, close enough 😄
yea
Zamestnanec
seems to be your employee, right?yea
BlazeBin - fkufwjmhdsqy
A tool for sharing your source code with the world!
divizia is divison
projekt is project
oddelenies is departement
right
firma is company
so, EF has some conventions
lets say your classes were called
Employee
and Division
if you had a EmployeeId
field in Division
, it would automatically understand that its a foreign key for a Employee
I also strongly recommend not using attributes to configure your models
and using the conventional namesokay
so what do u recommend
1 sec
take ur time
nothing is rushing me
something like this
obviously not all in one file 😄
but notice how all entities have an
int Id
property
and we can configure the divisionleader with the entity configurationthe employe the ? what does that mean
or any other ?
it means nullable
oh
it means that a division might not have a leader
some do, some dont
and if the leader is deleted, we automatically set this value to null to indicate that the division has no leader then
oooh okay thats why it had a problem with cascading ? or the on update no delete
yeah
SQL isnt trivial, relationships must be guaranteed to always be valid and there are a ton of rules and checks in place to ensure that
and that can often get confusing when you are new
yea i was kinda confused since i only worked with c++ and java so far
when I first started out with SQL 20+ years ago, we didnt have fancy tools like EF either
I wrote my sql manually and just skipped foreign keys for a long time 😄
daaamn must have been hard 😄
well im happy that there are tools for that nowadays
in a way it was great tbh
it means I actually know why those errors appear
to be fair I love EF and would highly recommend using it
but it also means the sql layer is "hidden" and when you get an error like this its not obvious what you need to do to configure it
but yeah, use
IEntityTypeConfiguration<T>
to configure your entities, not attributes
it gives you much more optionsalright im gonna implement the changes after that ill have to figure out how to do front end hah
gl!
thanks a bunch