0
votes

Im trying to avoid cascading deleting by changing the Id of the delete item to null for all "Cats" that have the CatLitter Id. can someone tell me what i need to do. this is what i come up with so far im pretty sure im somewhere in the right direction.

Problem: when i delete CatLitter - All Cats that have CatLitterId also gets deleted.

    public ActionResult CatLittersDelete(int Id)
    {
        CatLitter ni = db.CatLitters.Find(Id);
        foreach (var item in ni.Cats.Where(m => m.CatLitterId == Id))
        {
            item.CatLitterId = null;
            db.Entry(item).State = EntityState.Modified;
        }
        db.CatLitters.Remove(ni);
        db.SaveChanges();
        return RedirectToAction("CatPropCreate");
    }

Model:

public class Cat
{
    public int Id { get; set; }
    public string Name { get; set; } 
    public int? CatLitterId { get; set; }
    public virtual CatLitter CatLitters { get; set; }

Using Entity Framework Code First.

So im trying this now from what u guys have given me:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Cat>()
            .HasOptional<CatLitter>(s => s.CatLitters)
            .WithMany()
            .WillCascadeOnDelete(false);
    }

but im getting this when i try update db.

One or more validation errors were detected during model generation:

CatProject.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. CatProject.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined. IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined.

2
You have WillCascadeOnDelete(false), but is cascaded delete off in the database? I bet it isn't. The exception is about IdentityUserRole, so it's not related to your question.Gert Arnold

2 Answers

0
votes

Try this in your model:

modelBuilder.Entity<CatLitter>()
        .HasOne(p => p.Cat)
        .WithMany(b => b.CatLitters)
        .OnDelete(DeleteBehavior.Restrict);

https://docs.microsoft.com/en-us/ef/core/modeling/relationships

https://docs.microsoft.com/en-us/ef/core/api/microsoft.entityframeworkcore.metadata.deletebehavior

Edit: Changing the ID to null will probably not have the desired effect. Try:

context.Remove(CatLitter);
context.SaveChanges();
0
votes

i solved it by adding this code.

public ActionResult CatLittersDelete(int? Id)
    {
        CatLitter ni = db.CatLitters.Find(Id);
        if (Id != null)
        {
            foreach (var item in ni.Cats.Where(m => m.CatLitterId == Id).ToList())
            {
                item.CatLitterId = null;
                db.Entry(item).State = EntityState.Modified;
                db.SaveChanges();
            }
        }

        db.CatLitters.Remove(ni);
        db.SaveChanges();
        return RedirectToAction("CatPropCreate");
    }

still feel like its working the way around the problem i would love to know how to use the

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Cat>()
        .HasOptional<CatLitter>(s => s.CatLitters)
        .WithMany()
        .WillCascadeOnDelete(false);
}

properly if anyone knows how to make that work i would appreciate it