OnPropertyChange 作为实体框架中触发器的替代方案

发布于 2024-08-28 01:54:40 字数 338 浏览 3 评论 0原文

我是实体框架的新手,请原谅我的无知。我们有一个使用 SQL Server 和 ADO.Net 编写的简单应用程序。现在我们有了一个新的要求,每当我们的应用程序更新特定行时,就必须发生一些业务逻辑。我一直在研究 SQL Server 中的触发器,看起来我们可以使用触发器来做到这一点。我还在研究实体框架的 OnPropertyChange 功能。是否可以通过“OnPropertyChange”来做到这一点?

我的意思是,我可以创建一个模型并实现“OnPropertyChange”方法吗?当我们的应用程序使用 ADO.Net 修改行时,它会触发“OnPropertyChange”事件,以便执行“OnPropertyChange”中的客户代码吗?

I am new to Entity Framework and please pardon my ignorance. We have a simple application written using SQL Server and ADO.Net. Now we have got a new requirement that, whenever a particular rows get updated by our application, some business logic has to happen. I have been looking into Triggers in SQL server and it looks like we can do it using triggers. I am also looking at Entity Framework's OnPropertyChange capability. Is it possible to do it with "OnPropertyChange"?

I mean, Can I create a model and implement "OnPropertyChange" method? And when our application modifies the rows using ADO.Net, then will it fire "OnPropertyChange" event so that my custome code in "OnPropertyChange" executes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-09-04 01:54:40

每当数据库表本身发生操作时就会发生触发器。如果您执行更新并具有更新触发器,则无论您是使用实体框架还是直接针对该表编写更新语句,都会发生这种情况。以下是关于SQL Server 触发器的参考。

编辑
如果您希望发生与数据库相关或不相关的自定义业务逻辑,您可以执行以下操作:

    private string _myProperty;    
    public string MyProperty
    {
        get 
        {
            return _myProperty;
        }
        set
        {
              if (_myProperty != value)
              {
                 _myProperty = _value;
                 OnPropertyChanged("MyProperty");
                 DoSomethingWithMyProperty(_myProperty);
               }
          }
     }

DoSomethingWithMyProperty 方法中,您可以执行任何您想要的操作,无论是更新您的 UI或数据库。

Triggers occur whenever an action happens on the database table itself. If you perform an update and have an update trigger, this will happen regardless of whether you use Entity Framework or write an update statement directly against that table. Here is a reference on SQL Server Triggers.

EDIT
If you want custom business logic to occur that either pertains or doesn't pertain to the database you can do the following:

    private string _myProperty;    
    public string MyProperty
    {
        get 
        {
            return _myProperty;
        }
        set
        {
              if (_myProperty != value)
              {
                 _myProperty = _value;
                 OnPropertyChanged("MyProperty");
                 DoSomethingWithMyProperty(_myProperty);
               }
          }
     }

In the DoSomethingWithMyProperty method you can do whatever you would like, whether it be update your UI or database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文