1

I am writing a piece of software that will act as a middle man between some warehouse management software and automated vehicle hardware. My program has an entity model of a DB that lives on a server.

The vehicle hardware we are using comes with some software that allows it to read and write to a DB.

I would like to set up events in my entity models that would fire when a particular field is updated.

In example: The vehicle might encounter an error, it would then write some values to a table in the DB. My program then needs to recognize this change and inform the WM software.

This solution needs to be event driven as I can not sacrifice CPU to constantly poll the DB for changes

I have seen many solutions to this by overriding the SaveChanges() method for the entity, however it appears to me that would only work if the changes are coming from the application itself, not from an outside source.

Is this possible? Does EF have this built in and im just not seeing it?

When the physical DB updates does EF call saveChanges()?

Many Thanks

Swoody17
  • 23
  • 5
  • I am not sure that such things are even possible. May be reasonable to add some code directly to your DB: https://msdn.microsoft.com/en-us/library/ms254498(v=vs.110).aspx. This code can use some system events, .net remoting or something else to deliver 'event' to your software. As I know, SQL Server supports notifications but for DDL only. – Andrii Shvydkyi Feb 13 '15 at 15:38
  • My software is a dll... – Swoody17 Feb 13 '15 at 16:42
  • If you can call your DLL from database trigger - you have the solution. Am I correct? – Andrii Shvydkyi Feb 14 '15 at 14:38
  • SQL Server Broker and .net's SqlDependancy class will help you. – zaitsman Feb 15 '15 at 02:36

2 Answers2

1

Based on this answer to a similar question, the answer appears to be no at least directly within Entity Framework. As mentioned by @zaitsman in a comment, you might be able to use the SqlDependancy class. The documentation has this example that might be helpful:

void SomeMethod()
{
    // Assume connection is an open SqlConnection.

    // Create a new SqlCommand object.
    using (SqlCommand command=new SqlCommand(
        "SELECT ShipperID, CompanyName, Phone FROM dbo.Shippers",
        connection))
    {

        // Create a dependency and associate it with the SqlCommand.
        SqlDependency dependency=new SqlDependency(command);
        // Maintain the reference in a class member.

        // Subscribe to the SqlDependency event.
        dependency.OnChange+=new
           OnChangeEventHandler(OnDependencyChange);

        // Execute the command.
        using (SqlDataReader reader = command.ExecuteReader())
        {
            // Process the DataReader.
        }
    }
}
John Cummings
  • 1,949
  • 3
  • 22
  • 38
0

In order to build a message-driven application in .NET, you need to use some sort of ESB (enterprise service bus) architecture. There are many technologies that work with .NET but Entity Framework will not suffice here. Look into: Rabbit MQ, Azure ESB, BizTalk or similar technologies that support messaging. Basically a message needs to be fired when data changes.

elecstrat
  • 52
  • 3