Home > Back-end >  i want to delete value(s) from parent table but i got exception
i want to delete value(s) from parent table but i got exception

Time:12-29

Hi i want to develop web app with asp.net, i have foreign key and i want to delete from parent table but i have exception why i got this exception. i add code details.please help me

parent class

 public class Calisan:IEntity
{
    public int CalisanId { get; set; }
    public string TcNo { get; set; }
    public string Isim { get; set; }
    public string Soyisim { get; set; }
}}

child class

public  class Hastalik:IEntity
{
    public int HastalikId { get; set; }
    public int CalisanId { get; set; }
    public string HastalikIsmi { get; set; }
    public DateTime HastalikTarihi { get; set; }
    
}

t-sql

    CREATE TABLE [dbo].[Hastaliklar] (
    [HastalikId]     INT          NOT NULL,
    [CalisanId]      INT          NULL,
    [HastalikIsmi]   VARCHAR (50) NOT NULL,
    [HastalikTarihi] DATE         NOT NULL,
    PRIMARY KEY CLUSTERED ([HastalikId] ASC),
    CONSTRAINT [FK_Hastaliklar_Calisanlar] FOREIGN KEY ([CalisanId]) REFERENCES [dbo].[Calisanlar] ([CalisanId])
);

my context

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=SirketDB;Trusted_Connection=true");
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
          modelBuilder.Entity<Hastalik>()
          .HasOne<Calisan>()
          .WithMany()
          .HasForeignKey(p => p.CalisanId)
          .OnDelete(DeleteBehavior.Cascade);
       
    }
    public DbSet<Calisan> Calisanlar { get; set;  }
    public DbSet<Hobii> Hobiler { get; set; }
    public DbSet<Hastalik> Hastaliklar { get; set; }        
    public DbSet<Covid> Covids { get; set; }
    public DbSet<Mesai> Mesailer { get; set; }
    public DbSet<Ilac> Ilaclar { get; set; }
    public DbSet<Temasli> Temaslilar { get; set; }
    public DbSet<Sempton> Semptonlar { get; set; }
    public DbSet<CovidBelirti> CovidBelirtiler { get; set; }
    public DbSet<Asi> Asilar { get; set; }
    public DbSet<KronikHastalik> KronikHastaliklar { get; set; }

EfEntityRepositoryBase

 public void Delete(TEntity entity)
    {
        using (TContext context = new TContext())
        {
           

            var deletedEntity = context.Entry(entity);
            deletedEntity.State = EntityState.Deleted;
//exception                context.SaveChanges();

        }
    }

exception

SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_Hastaliklar_Calisanlar". The conflict occurred in database "SirketDB", table "dbo.Hastaliklar", column 'CalisanId'. The statement has been terminated.

CodePudding user response:

From what I see, you can perform a truncate statement on "SirketDB", table "dbo.Hastaliklar" as there are data records that rely on the data column that you are attempting to delete.

You can either run a truncate query or DELETE FROM dbo.Hastaliklar query on SirketDB and your statement should work.

CodePudding user response:

Improve the classes relations by making CalisanId nullable and .OnDelete(DeleteBehavior.ClientSetNull)

 public class Calisan:IEntity
{
    public int CalisanId { get; set; }
    public string TcNo { get; set; }
    public string Isim { get; set; }
    public string Soyisim { get; set; }
    public virtual  List <Hastalik> Hastaliks {get; set;}
}}

public  class Hastalik:IEntity
{
    public int HastalikId { get; set; }
    public int? CalisanId { get; set; }
    public string HastalikIsmi { get; set; }
    public DateTime HastalikTarihi { get; set; }
    
}

 modelBuilder.Entity<Hastalik>()
          .HasOne<Calisan>()
          .WithMany()
          .HasForeignKey(p => p.CalisanId)
          .OnDelete(DeleteBehavior.ClientSetNull);

IMHO delete method should be fixed too

 public void Delete(T entity)
    {
        using (TContext context = new TContext())
        {
            var deletedEntity = context.Set<T>().Find(entity);
          context.Set<T>().Remove(deletedEntity);
           context.SaveChanges();

        }
    }
  • Related