实体框架监听SQL Server的变化

时间:2021-05-02 10:05:51

I'm working on the following scenario:

我正在研究以下场景:

I have a console up that populates a SQL Server database with some data. I have one more web app that reads the same database and displays the data on a front-end. Both of the applications use Entity Framework to communicate with the database (they have the same connection string).

我有一个控制台,用一些数据填充SQL Server数据库。我还有一个Web应用程序读取同一个数据库并在前端显示数据。两个应用程序都使用Entity Framework与数据库通信(它们具有相同的连接字符串)。

I wonder how can the web app be notified for any changes that have occurred to the database. Bear in mind that the two applications are not referenced, whatsoever.

我想知道如何通知Web应用程序数据库发生的任何更改。请记住,两个应用程序都没有被引用。

Is there event provided by EF that fires when some has changes. In essence, I would like to know when a change has happened, as well as, the nature of that change

是否有EF提供的事件会在某些事件发生变化时触发。从本质上讲,我想知道何时发生了变化,以及变化的性质

2 个解决方案

#1


1  

I had a similar requirement and I solved it using the EF function:

我有类似的要求,我使用EF函数解决了它:

[context].Database.CompatibleWithModel(throwIfNoMetadata: true)

It will return if your model matches the underlying database structure using the metadata table.

如果您的模型使用元数据表与基础数据库结构匹配,它将返回。

Note that I was using a Code First approach.

请注意,我使用的是Code First方法。

The msdn definition is below:

msdn定义如下:

http://msdn.microsoft.com/en-us/library/system.data.entity.database.compatiblewithmodel(v=vs.103).aspx

http://msdn.microsoft.com/en-us/library/system.data.entity.database.compatiblewithmodel(v=vs.103).aspx

Edit:

编辑:

Just found an amazing article with a demonstration:

刚刚发现了一篇带有演示的精彩文章:

http://blog.oneunicorn.com/2011/04/08/code-first-what-is-that-edmmetadata-table/

http://blog.oneunicorn.com/2011/04/08/code-first-what-is-that-edmmetadata-table/

#2


0  

This is not something that is related to EF at all. EF is just a library that makes SQL calls and maps them to objects. It has no inside knowledge of the database. As such, when data changes in one application, another application doesn't know unless they query to see if that data changes (and you're not going to be constantly running queries to know that, it's too impractical).

这根本不是与EF有关的东西。 EF只是一个进行SQL调用并将它们映射到对象的库。它没有数据库的内部知识。因此,当一个应用程序中的数据发生更改时,另一个应用程序不会知道,除非他们查询该数据是否发生更改(并且您不会经常运行查询以了解该数据,这太不切实际了)。

There are, potentially some ways to do this, such as adding triggers to the database, which then call extended stored procs to send messages to the app, but this is a lot of work to go through, and it can possibly compromise the robustness of the database.

可能有一些方法可以做到这一点,例如向数据库添加触发器,然后调用扩展存储过程将消息发送到应用程序,但这需要经过很多工作,并且可能会损害数据库的健壮性。数据库。

There used to be something called Notification Services, but that was deprecated. There's now something called SqlDependency objects, which may help you in some cases.. but it all depends on what you're trying to do exactly.

曾经有一种名为Notification Services的东西,但已被弃用。现在有一些叫做SqlDependency对象的东西,在某些情况下可能对你有所帮助..但这一切都取决于你要做的事情。

In any event, it's usually easier to find a different way to do what you want. This is complex topic, and really requires a lot of sql server knowledge.

无论如何,通常更容易找到一种不同的方式来做你想做的事情。这是一个复杂的主题,并且确实需要很多sql server知识。

#1


1  

I had a similar requirement and I solved it using the EF function:

我有类似的要求,我使用EF函数解决了它:

[context].Database.CompatibleWithModel(throwIfNoMetadata: true)

It will return if your model matches the underlying database structure using the metadata table.

如果您的模型使用元数据表与基础数据库结构匹配,它将返回。

Note that I was using a Code First approach.

请注意,我使用的是Code First方法。

The msdn definition is below:

msdn定义如下:

http://msdn.microsoft.com/en-us/library/system.data.entity.database.compatiblewithmodel(v=vs.103).aspx

http://msdn.microsoft.com/en-us/library/system.data.entity.database.compatiblewithmodel(v=vs.103).aspx

Edit:

编辑:

Just found an amazing article with a demonstration:

刚刚发现了一篇带有演示的精彩文章:

http://blog.oneunicorn.com/2011/04/08/code-first-what-is-that-edmmetadata-table/

http://blog.oneunicorn.com/2011/04/08/code-first-what-is-that-edmmetadata-table/

#2


0  

This is not something that is related to EF at all. EF is just a library that makes SQL calls and maps them to objects. It has no inside knowledge of the database. As such, when data changes in one application, another application doesn't know unless they query to see if that data changes (and you're not going to be constantly running queries to know that, it's too impractical).

这根本不是与EF有关的东西。 EF只是一个进行SQL调用并将它们映射到对象的库。它没有数据库的内部知识。因此,当一个应用程序中的数据发生更改时,另一个应用程序不会知道,除非他们查询该数据是否发生更改(并且您不会经常运行查询以了解该数据,这太不切实际了)。

There are, potentially some ways to do this, such as adding triggers to the database, which then call extended stored procs to send messages to the app, but this is a lot of work to go through, and it can possibly compromise the robustness of the database.

可能有一些方法可以做到这一点,例如向数据库添加触发器,然后调用扩展存储过程将消息发送到应用程序,但这需要经过很多工作,并且可能会损害数据库的健壮性。数据库。

There used to be something called Notification Services, but that was deprecated. There's now something called SqlDependency objects, which may help you in some cases.. but it all depends on what you're trying to do exactly.

曾经有一种名为Notification Services的东西,但已被弃用。现在有一些叫做SqlDependency对象的东西,在某些情况下可能对你有所帮助..但这一切都取决于你要做的事情。

In any event, it's usually easier to find a different way to do what you want. This is complex topic, and really requires a lot of sql server knowledge.

无论如何,通常更容易找到一种不同的方式来做你想做的事情。这是一个复杂的主题,并且确实需要很多sql server知识。