C
C#•2mo 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
Maru•2mo 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
Maru•2mo ago
Pobiega
Pobiega•2mo ago
No zips or word documents please, either upload to github or similar or paste the relevant stuff on $paste
MODiX
MODiX•2mo 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•2mo 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
Maru•2mo ago
yes that is wha the error said but i lack the knowledge on how to fix it
Pobiega
Pobiega•2mo ago
check what foreignkeys you have on "DbDiviza"
Maru
Maru•2mo ago
i have 2 idfirm and id of employe
Pobiega
Pobiega•2mo 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
Maru•2mo 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•2mo ago
you can have a "division leader" just fine, but it needs to have ON DELETE NO ACTION
Maru
Maru•2mo 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•2mo ago
its not related to C# at all how are you making your database?
Maru
Maru•2mo ago
im using entity frame work and creating it there thru migrating
Pobiega
Pobiega•2mo ago
Okay, now we're getting somewhere What version of EF?
Maru
Maru•2mo ago
8.0.5
Pobiega
Pobiega•2mo ago
ok good, so modern EF Core
Maru
Maru•2mo ago
well i did Add-Migration InitialCreate
Pobiega
Pobiega•2mo ago
you'd do this via the EntityConfiguration api, either via a IEntityTypeConfiguration<T> or via OnModelConfiguring
Maru
Maru•2mo ago
and also update-database lemme check
Pobiega
Pobiega•2mo ago
can you show me your database context source? preferably via $code
MODiX
MODiX•2mo 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
Maru•2mo ago
wrong code
Pobiega
Pobiega•2mo ago
copy the URL from the pastebin 😄
Maru
Maru•2mo ago
XD yea
Maru
Maru•2mo ago
BlazeBin - iseweprgwemw
A tool for sharing your source code with the world!
Pobiega
Pobiega•2mo 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
Maru•2mo ago
BlazeBin - qdlvfqyrozfp
A tool for sharing your source code with the world!
Pobiega
Pobiega•2mo ago
and re-create it, after we fix your context
Maru
Maru•2mo ago
ok
Pobiega
Pobiega•2mo 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
Maru•2mo ago
ok sec
Pobiega
Pobiega•2mo ago
just a general recommendation, its recommended to keep your code in english not everyone understands romanian 😛
Maru
Maru•2mo ago
its slovak but im gonna start using eng i guess
Pobiega
Pobiega•2mo ago
ah, close enough 😄
Maru
Maru•2mo ago
yea
Pobiega
Pobiega•2mo ago
Zamestnanec seems to be your employee, right?
Maru
Maru•2mo ago
yea
Maru
Maru•2mo ago
BlazeBin - fkufwjmhdsqy
A tool for sharing your source code with the world!
Maru
Maru•2mo ago
divizia is divison projekt is project oddelenies is departement
Pobiega
Pobiega•2mo ago
right
Maru
Maru•2mo ago
firma is company
Pobiega
Pobiega•2mo 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
Maru•2mo ago
okay so what do u recommend
Pobiega
Pobiega•2mo ago
1 sec
Maru
Maru•2mo ago
take ur time nothing is rushing me
Pobiega
Pobiega•2mo 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
Maru•2mo ago
the employe the ? what does that mean or any other ?
Pobiega
Pobiega•2mo ago
it means nullable
Maru
Maru•2mo ago
oh
Pobiega
Pobiega•2mo 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
Maru•2mo ago
oooh okay thats why it had a problem with cascading ? or the on update no delete
Pobiega
Pobiega•2mo 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
Maru•2mo ago
yea i was kinda confused since i only worked with c++ and java so far
Pobiega
Pobiega•2mo 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
Maru•2mo ago
daaamn must have been hard 😄 well im happy that there are tools for that nowadays
Pobiega
Pobiega•2mo 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
Maru•2mo ago
alright im gonna implement the changes after that ill have to figure out how to do front end hah
Pobiega
Pobiega•2mo ago
gl!
Maru
Maru•2mo ago
thanks a bunch
Want results from more Discord servers?
Add your server
More Posts