我如何重写DeleteObject实体框架重写、实体、框架、DeleteObject

2023-09-06 19:10:23 作者:听云窃窃私语

我是全新的,以EF和我遇到的困难让我的头周围,但继承人的情景。我创建了一个项目,并补充我已经用于生成从pre-内置SQL数据库车型EDMX图。那贵妇人图已经产生code每个模型的一个方面对我来说。上下文看起来是这样的:

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);

它将从数据库中删除所述someCase。但是我想我是否覆盖delete方法,而不是删除对象,以便它标志着对象的属性为已删除来代替。即someCase.Delete = TRUE;该系统的下一个部分是prevent系统加载例删除= TRUE。基本上,我不希望被彻底删除只是隐藏在系统中的记录。

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.

推荐答案

在除了@Ladislav建议的选项,你也可以创建一个的 INSTEAD OF DELETE 触发你的表,该表重新映射删除在数据库服务器级别,或use SavingChanges以改变对象的的状态。

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.