Reputation: 789
Hi i'm totally new to EF and i'm having difficulties getting my head around it but heres the scenario. I've created a project and added a edmx diagram which i've used to generate models from a pre-built SQL database. That dame diagram has generated code for each model an a context for me. the context looks something like :
public partial class BuzzEntities : ObjectContext
{
public ObjectSet<Case> Cases
{
get
{
if ((_Cases == null))
{
_Cases = base.CreateObjectSet<Case>("Cases");
}
return _Cases;
}
}
ok so far this allows me to do things like
private Buzz.BuzzEntities db = new BuzzEntities();
db.Cases.DeleteObject(someCase);
which will delete the someCase from the database. However what i would like to do i override the delete method so that instead of deleting the object it marks a property of the object as deleted instead. i.e. someCase.Delete=true; The next part of the system is to prevent the system from loading cases with deleted=true. Basically i dont want the record ever deleted just hidden from the system.
Whats the best way to achieve this? I know i could manually change the field and just save the record instead of calling delete object but i dont want to worry about setting or hiding records in my UI i want the frame work to handle it for me.
Upvotes: 1
Views: 1075
Reputation: 126567
In addition to the options @Ladislav suggests, you could also create an INSTEAD OF DELETE trigger for your table which remaps the delete at the DB server level, or use SavingChanges to alter the state of the object.
Upvotes: 1
Reputation: 364349
You cannot override delete operation. You can create custom stored procedure and map it to delete operation of your entity. The second part of your problem can be achieved by using conditional mapping where only entities with deleted != true will be used.
Upvotes: 0