24373

How do I override DeleteObject in Entity Framework

Question:

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.

Answer1:

In addition to the options @Ladislav suggests, you could also create an <a href="http://msdn.microsoft.com/en-us/library/ms191208.aspx" rel="nofollow">INSTEAD OF DELETE</a> trigger for your table which remaps the delete at the DB server level, or <a href="https://stackoverflow.com/questions/1942532/soft-deletes-ishistorical-column-with-entityframework/1946339#1946339" rel="nofollow">use SavingChanges to alter the state of the object</a>.

Answer2:

You cannot override delete operation. You can create custom stored procedure and <a href="http://msdn.microsoft.com/en-us/library/cc716711.aspx" rel="nofollow">map it to delete operation</a> of your entity. The second part of your problem can be achieved by using <a href="http://www.codeproject.com/Articles/94784/Using-Conditional-Mapping-in-Entity-Framework" rel="nofollow">conditional mapping</a> where only entities with deleted != true will be used.

Recommend

  • Rails4: To check for no image uploaded with carrierwave is not working when delete the image
  • ObjectSet library is not being found?
  • Save disconnected object in entity framework 4
  • Finding the related FK property of a Navigation Property
  • How to query property value when property name is a parameter?
  • express.static handling root url request
  • Pasting URLs That Have Been Scraped From a Webpage
  • Watir-webdriver timing out when asked if element is present?
  • ObjectMaterialize in EF not firing on first level query
  • Java ClassLoader Confusion
  • Writing a recursive function on lists in Haskell
  • WCF Data Services Entity Framework
  • Issue with std::thread when using g++ in 32-bit MinGW 4.8.0
  • overlapping appointments using the entity framework
  • jQueryMobile, Ajax Navigation, and MVC
  • calculating number of bytes of each row in an image
  • Why isn't my “Fizz Buzz” test in R working?
  • crash in __tcf_0
  • For loop with if condition on multiple R functions
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • Content-Length header not returned from Pylons response
  • output of program is not same as passed argument
  • Javascript + PHP Encryption with pidCrypt
  • PHP: When would you need the self:: keyword?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Getting Messege Twice Using IMvxMessenger
  • LevelDB C iterator
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?