C
C#•8mo ago
Maru

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
Maru
MaruOP•8mo ago
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
Maru
MaruOP•8mo ago
Pobiega
Pobiega•8mo ago
No zips or word documents please, either upload to github or similar or paste the relevant stuff on $paste
MODiX
MODiX•8mo ago
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!
Pobiega
Pobiega•8mo ago
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
Maru
MaruOP•8mo ago
yes that is wha the error said but i lack the knowledge on how to fix it
Pobiega
Pobiega•8mo ago
check what foreignkeys you have on "DbDiviza"
Maru
MaruOP•8mo ago
i have 2 idfirm and id of employe
Pobiega
Pobiega•8mo ago
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?
Maru
MaruOP•8mo ago
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
Pobiega
Pobiega•8mo ago
you can have a "division leader" just fine, but it needs to have ON DELETE NO ACTION
Maru
MaruOP•8mo ago
yea but i have no clue how to do that in c# XD is it like a parameter or a method or
Pobiega
Pobiega•8mo ago
its not related to C# at all how are you making your database?
Maru
MaruOP•8mo ago
im using entity frame work and creating it there thru migrating
Pobiega
Pobiega•8mo ago
Okay, now we're getting somewhere What version of EF?
Maru
MaruOP•8mo ago
8.0.5
Pobiega
Pobiega•8mo ago
ok good, so modern EF Core
Maru
MaruOP•8mo ago
well i did Add-Migration InitialCreate
Pobiega
Pobiega•8mo ago
you'd do this via the EntityConfiguration api, either via a IEntityTypeConfiguration<T> or via OnModelConfiguring
Maru
MaruOP•8mo ago
and also update-database lemme check
Pobiega
Pobiega•8mo ago
can you show me your database context source? preferably via $code
MODiX
MODiX•8mo ago
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/
Maru
MaruOP•8mo ago
wrong code
Pobiega
Pobiega•8mo ago
copy the URL from the pastebin 😄
Maru
MaruOP•8mo ago
XD yea
Maru
MaruOP•8mo ago
BlazeBin - iseweprgwemw
A tool for sharing your source code with the world!
Pobiega
Pobiega•8mo ago
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
Maru
MaruOP•8mo ago
BlazeBin - qdlvfqyrozfp
A tool for sharing your source code with the world!
Pobiega
Pobiega•8mo ago
and re-create it, after we fix your context
Maru
MaruOP•8mo ago
ok
Pobiega
Pobiega•8mo ago
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
Maru
MaruOP•8mo ago
ok sec
Pobiega
Pobiega•8mo ago
just a general recommendation, its recommended to keep your code in english not everyone understands romanian 😛
Maru
MaruOP•8mo ago
its slovak but im gonna start using eng i guess
Pobiega
Pobiega•8mo ago
ah, close enough 😄
Maru
MaruOP•8mo ago
yea
Pobiega
Pobiega•8mo ago
Zamestnanec seems to be your employee, right?
Maru
MaruOP•8mo ago
yea
Maru
MaruOP•8mo ago
BlazeBin - fkufwjmhdsqy
A tool for sharing your source code with the world!
Maru
MaruOP•8mo ago
divizia is divison projekt is project oddelenies is departement
Pobiega
Pobiega•8mo ago
right
Maru
MaruOP•8mo ago
firma is company
Pobiega
Pobiega•8mo ago
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 names
Maru
MaruOP•8mo ago
okay so what do u recommend
Pobiega
Pobiega•8mo ago
1 sec
Maru
MaruOP•8mo ago
take ur time nothing is rushing me
Pobiega
Pobiega•8mo ago
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace TestEfApp.Data;

public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}

public DbSet<Division> Divisions { get; private set; }
public DbSet<Company> Companies { get; private set; }
public DbSet<Employee> Employees { get; private set; }


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);

base.OnModelCreating(modelBuilder);
}
}

public class DivisionConfiguration : IEntityTypeConfiguration<Division>
{
public void Configure(EntityTypeBuilder<Division> builder)
{
builder
.HasOne<Employee>(x => x.DivisionLeader)
.WithMany()
.HasForeignKey(x => x.DivisionLeaderId)
.OnDelete(DeleteBehavior.SetNull);
}
}

public class Division
{
public int Id { get; set; }

public int CompanyId { get; set; }

public Company Company { get; set; }

public string Name { get; set; }

public int? DivisionLeaderId { get; set; }

public Employee? DivisionLeader { get; set; }
}

public class Employee
{
public int Id { get; set; }

public string Name { get; set; }
}

public class Company
{
public int Id { get; set; }

public string Name { get; set; }
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace TestEfApp.Data;

public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}

public DbSet<Division> Divisions { get; private set; }
public DbSet<Company> Companies { get; private set; }
public DbSet<Employee> Employees { get; private set; }


protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);

base.OnModelCreating(modelBuilder);
}
}

public class DivisionConfiguration : IEntityTypeConfiguration<Division>
{
public void Configure(EntityTypeBuilder<Division> builder)
{
builder
.HasOne<Employee>(x => x.DivisionLeader)
.WithMany()
.HasForeignKey(x => x.DivisionLeaderId)
.OnDelete(DeleteBehavior.SetNull);
}
}

public class Division
{
public int Id { get; set; }

public int CompanyId { get; set; }

public Company Company { get; set; }

public string Name { get; set; }

public int? DivisionLeaderId { get; set; }

public Employee? DivisionLeader { get; set; }
}

public class Employee
{
public int Id { get; set; }

public string Name { get; set; }
}

public class Company
{
public int Id { get; set; }

public string Name { get; set; }
}
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 configuration
Maru
MaruOP•8mo ago
the employe the ? what does that mean or any other ?
Pobiega
Pobiega•8mo ago
it means nullable
Maru
MaruOP•8mo ago
oh
Pobiega
Pobiega•8mo ago
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
Maru
MaruOP•8mo ago
oooh okay thats why it had a problem with cascading ? or the on update no delete
Pobiega
Pobiega•8mo ago
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
Maru
MaruOP•8mo ago
yea i was kinda confused since i only worked with c++ and java so far
Pobiega
Pobiega•8mo ago
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 😄
Maru
MaruOP•8mo ago
daaamn must have been hard 😄 well im happy that there are tools for that nowadays
Pobiega
Pobiega•8mo ago
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 options
Maru
MaruOP•8mo ago
alright im gonna implement the changes after that ill have to figure out how to do front end hah
Pobiega
Pobiega•8mo ago
gl!
Maru
MaruOP•8mo ago
thanks a bunch
Want results from more Discord servers?
Add your server