0 Replies - 186 Views - Last Post: 13 September 2012 - 04:35 AM Rate Topic: -----

#1 MrShoes  Icon User is offline

  • D.I.C Regular

Reputation: 186
  • View blog
  • Posts: 303
  • Joined: 13-June 12

Entity Framework - A better way to achieve this?

Posted 13 September 2012 - 04:35 AM

Let me set the scene to give this some context.

I have a device "Remote" which is gathering data from a number of sensors. There is a Windows service "Client" running which monitors if any data needs to be sent. If it does, it converts the data into Data Transfer Objects, then attaches them to Message objects (all with the same Interface IMessageBase) which are sent through middleware to a cloud based service "Server" that saves the data to a server-side database.

The Data Access Layers are dealt with by Entity Framework 4.1, and because the databases are different, two different contexts are made. That's fine, because they both inherit from DbContext.

One thing that has to happen is that when the Remote device gets acknowledgement from the middleware that the message has been published, it needs to be removed from the staging table in the Remote database (Change_Staging). The same is true if any message is sent from the Server to the Remote Client. That means the message needs a method to delete, which I've implemented. But... surely there's a better way then this? Any suggestions welcome.

public void DeleteFromDatabase(DbContext context)
        {
            if (context is RemoteEntities)
            {
                var temp = context.Set(typeof(RemoteDal.Change_Staging)).Find(StagingId);
                context.Entry(temp).State = EntityState.Deleted;
            }
            else if (context is ServerEntities)
            {
                var temp = context.Set(typeof(ServerDal.Change_Staging)).Find(StagingId);
                context.Entry(temp).State = EntityState.Deleted;
            }
        }


Is This A Good Question/Topic? 0
  • +

Page 1 of 1