how to force delete rows/record of "mapping" table using LINQ to solve the exception.
I am using MySQL DB and there are 3 tables as below:
1. Table "Mapping": mapping_id (primary key), mapping_name, mapping_type, mapping_status
2. Table "MappingDetails": id (primary key), mapping_id (foreign key), md_name, md_type, md_status
3. Table "C": id (primary key), mapping_id (foreign key), tableC_name, tableC_type, tableC_status
I am using LINQ to delete the record(s) of "Mapping" table, but since mapping_id is a foreign key column in other tables, I get below exception while delete:
{"The association between entity types 'Mapping' and 'MappingDetails' has been severed but the relationship is either marked as 'Required'
or is implicitly required because the foreign key is not nullable. If the dependent/child entity should be deleted when a required relationship
is severed, then setup the relationship to use cascade deletes. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging'
to see the key values."}
My delete C# code is as below:
var getMappingId = (from x in _repository.GetAllMappingDetails().ToList()
where x.Id == Id
select x.MappingId
).FirstOrDefault();
Mapping m = _repository.GetAllMapping()
.Where(m => m.MappingId == getMappingId)
.FirstOrDefault<Mapping>();
_context.Mapping.Remove(m);
context.SaveChanges();
Thank you in advance