I'm using EF 4.0 and DD. I turned off cascading deleting on some of my tables. I want to capture the exception and post an error message telling the user to delete the associated
records. I thought I could do this using a partial class in my Metadata section but I have not had much luck. Any ideas?
The DELETE statement conflicted with the REFERENCE constraint "FK_Room_RoomLevel1". The conflict occurred in database "WEB_RoomAudit", table "dbo.Room", column 'idRoomLevel'.
The statement has been terminated.
class Customer < ActiveRecord::Base
has_many :orders
before_destroy :check_for_orders
private
def check_for_orders
if orders.count > 0
errors.add_to_base("cannot delete customer while orders exist")
return false
end
end
end
The DELETE statement conflicted with the REFERENCE constraint "FK_Room_RoomLevel1". The conflict occurred in database "WEB_RoomAudit", table "dbo.Room", column 'idRoomLevel'. The statement has been terminated.
This error means that you've tried to delete the some key, but the key is referred by the other tables and it's a foreign table, so foreign keys should be deleted……
Suggetion is that you should create Cascading Deleting:
Hi cooper, I do this by interception the Save changes of the dontaxe like this:
public partial class WorkloadEntities
{
partial void OnContextCreated()
{
// Register the handler for the SavingChanges event.
this.SavingChanges += new EventHandler(context_SavingChanges);
}
public override int SaveChanges(System.Data.Objects.SaveOptions options)
{
try
{
return base.SaveChanges(options);
}
catch (Exception exception)
{
// Find the most innermost exception, unwrap it.
var message = exception.ExtractExceptionMessage();
if (exception is UpdateException && message.StartsWith("Violation of UNIQUE KEY constraint"))
throw new ValidationException("You cannot create duplicate records.");
//TODO: build logging and notification in here
throw new ValidationException(message);
}
}
}
public static class ExceptionExtensionMethods
{
public static String ExtractExceptionMessage(this Exception exception)
{
while (exception.InnerException != null)
exception = exception.InnerException;
return exception.Message;
}
}
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Marked as answer by cooper88 on Jul 17, 2012 09:31 PM
cooper88
Member
116 Points
103 Posts
Preventing the deletion of a record that has associated rows
Jul 13, 2012 07:45 PM|LINK
I'm using EF 4.0 and DD. I turned off cascading deleting on some of my tables. I want to capture the exception and post an error message telling the user to delete the associated records. I thought I could do this using a partial class in my Metadata section but I have not had much luck. Any ideas?
The DELETE statement conflicted with the REFERENCE constraint "FK_Room_RoomLevel1". The conflict occurred in database "WEB_RoomAudit", table "dbo.Room", column 'idRoomLevel'. The statement has been terminated.
Aparnathi ka...
Member
116 Points
23 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 14, 2012 11:16 AM|LINK
Try with this example:
Dan Bracuk
Contributor
3970 Points
1096 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 14, 2012 11:37 AM|LINK
Rather than telling the user to delete the associated rows, why don't you do it for him?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 15, 2012 01:54 AM|LINK
This error means that you've tried to delete the some key, but the key is referred by the other tables and it's a foreign table, so foreign keys should be deleted……
Suggetion is that you should create Cascading Deleting:
cooper88
Member
116 Points
103 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 15, 2012 08:20 PM|LINK
I don't want delete cascading. I want them to delete all the records that belong to the foreign key first.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 16, 2012 12:53 AM|LINK
Hi,
Then I think you have to fetch all the records that belong to a certain record model entity instance and then delete them one by one.
sjnaughton
All-Star
27391 Points
5485 Posts
MVP
Re: Preventing the deletion of a record that has associated rows
Jul 16, 2012 11:31 AM|LINK
Hi cooper, I do this by interception the Save changes of the dontaxe like this:
public partial class WorkloadEntities { partial void OnContextCreated() { // Register the handler for the SavingChanges event. this.SavingChanges += new EventHandler(context_SavingChanges); } public override int SaveChanges(System.Data.Objects.SaveOptions options) { try { return base.SaveChanges(options); } catch (Exception exception) { // Find the most innermost exception, unwrap it. var message = exception.ExtractExceptionMessage(); if (exception is UpdateException && message.StartsWith("Violation of UNIQUE KEY constraint")) throw new ValidationException("You cannot create duplicate records."); //TODO: build logging and notification in here throw new ValidationException(message); } } } public static class ExceptionExtensionMethods { public static String ExtractExceptionMessage(this Exception exception) { while (exception.InnerException != null) exception = exception.InnerException; return exception.Message; } }Always seeking an elegant solution.
cooper88
Member
116 Points
103 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 17, 2012 08:19 PM|LINK
Found a full working example that catches all execptions that override ValidateException.
http://blogs.msdn.com/b/davidebb/archive/2008/12/11/handling-database-exceptions-in-dynamic-data.aspx
cooper88
Member
116 Points
103 Posts
Re: Preventing the deletion of a record that has associated rows
Jul 17, 2012 08:40 PM|LINK
namespace AuditDatabaseModel { public partial class AuditDatabaseEntities { /* put code above here, I used a seprate file in my app_Code directory */