EF Core delete on table violates foreign key constraint on table

entity framework core cascade delete not working
entity framework cascade delete one to many
ef core one-to-one cascade delete
entity framework delete record with foreign key constraint
entity framework delete object with relationships
ef core willcascadeondelete
ef core nullable foreign key
ef core recursive delete

I have two tables (Device and PropertyValue) and when I try to delete a Device I get an exception saying:

violates foreign key

I also tried to set the cascade on OnModelCreating(), but still not work.

public class Device
{
    public int Id { get; set; }
    public List<PropertyValue> Properties { get; set; }
}

public class PropertyValue
{
    public int Id { get; set; }
    public int? DeviceId { get; set; }
    public Device Device { get; set; }
}

modelBuilder.Entity<PropertyValue>()
    .HasOne(p => p.Device)
    .WithMany(b => b.Properties)
    .HasForeignKey(w => w.DeviceId)
    .OnDelete(DeleteBehavior.Cascade);  

EDIT: I am using a repository but basally I am deleting using _context.Devices.Remove(entity);


Since the FK property DeviceId is nullable, the relationship is optional, which by default has delete cascade turned off.

So adding .OnDelete(DeleteBehavior.Cascade) is step in the right direction.

But if you haven't done that initially and have already created your tables and relationships, you have to make sure it's applied in the database as well (because for cascade delete EF relies in general to be implemented by the database), by generating new migration and updating the database.

[SOLVED], Since the FK property DeviceId is nullable, the relationship is optional, which by default has delete cascade turned off. So adding . I have two tables (Device and PropertyValue) and when I try to delete a Device I get an exception saying:violates foreign key. I also tried to set the cascade on OnModelCreating(), but still not work.


If the related entities in the Properties collection are not loaded and tracked by the context, they will not be deleted. The recommended approach is to set a cascade action on the foreign key constraint in the database as well, as described in the documentation for DeleteBehavior:

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

If you create the schema automatically with EF, this is also what will be generated.

[SOLVED], violates foreign key constraint. Id); table.ForeignKey( name: "​FK_Cities_ZipCodes_ZipCodeId", column: x => x. As you can see, the FK is created with cascade delete turned on. Also there is no unique constraint/index on ZipCodeId column, which indicates that EF Core assumes one-to-many  PostgresException: 23503: Insert or update on table "product_extension" violates foreign key constraint "FK_product_extension_product_product_id" So product is not created first and product id assigned to productextesion.productid? Do I need to do an Add and SaveChanges for each table?


The line

public int? DeviceId { get; set; } 

defines a property name DeviceId, and this property allows null. Try to remove the ? character from that and delete the part .HasForeignKey(w => w.DeviceId). It should work.

Cascade Delete, Because there is no foreign key property in the dependant Book entity, EF Core will foreign key column in the Books table with a referential constraint action of that so that the foreign key field is set to null when the principal is deleted:. Foreign key with Cascade DELETE In Entity Framework Core, by default the foreign keys in AspNetUserRoles table have Cascade DELETE behaviour. This means, if a record in the parent table


The Fluent API OnDelete Method, ProjectPages_FooterId' on table 'ProjectPages' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE  ERROR: insert or update on table "Table3" violates foreign key constraint "Table3_DataID_fkey" DETAIL: Key (DataID)=(27856) is not present in table "Table1". So simple: if there is no row in Table1 where DataID = 27856, then you can't insert that row into Table3.


Entity Framework Cascade delete, npgsql / efcore.pg but still get "update or delete on table violates foreign key constraint" #1043. Open EF Core version: Microsoft. None. None. In the tables above, None can result in a constraint violation. For example, if a principal/child entity is deleted but no action is taken to change the foreign key of a dependent/child, then the database will likely throw on SaveChanges due to a foreign constraint violation.


Set DeleteBehavior.ClientNoAction but still get "update or delete on , I have two tables having one of them an auto-generated serial id: FOREIGN KEY (app_id) REFERENCES app(app_id) ON DELETE RESTRICT; insert or update on table "app_user" violates foreign key constraint once that's properly done, EF Core will issue the insert statements in the right order. Using Relational Database - Create a relation between a table having Primary Key and a table having Foreign Key with Delete constrain. This will delete the records in Child Table if a record in a Parent Table has been deleted.