4

I would like to do a .NET Entity Framework health check on all the existing Data Model Entities and potentially stored procedures (against their representative database tables) on application startup. We are using Database First Approach so changes are made in production using SQL scripts prone to manual execution errors.

This will allow me to identify any synchronization issues (Especially after deployment) in a controlled way (Customized error handling). Providing a higher level of confidence in new release deployments and faster debugging of issues.

Additionally this will be added to a self-diagnostic screen so that infrastructure personnel can at any time verify the database health.

Any idea how to do this? I can't seem to find a native EF mechanism of doing this so it will fail when you use the erroneous entity which is unpredictable and can easily be missed.

Marius Vorster
  • 196
  • 1
  • 10
  • 1
    This thread may give you some ideas http://stackoverflow.com/questions/11920491/migration-using-model-first-approach-in-entity-framework – Steve Greene Jul 06 '15 at 15:41
  • Thanks Steve, it was a good read and will play around with Huagati DBML/EDMX and some of the other comparison tools mentioned but I can't find any solution for EDMX database first run-time comparison. It is beginning to look like there is no way of doing it. – Marius Vorster Jul 07 '15 at 11:00
  • Switching to code first may not be as bad as it seems. Sounds like the EDMX is going away in future versions. http://thedatafarm.com/data-access/what-about-edmx-when-ef7-arrives/ – Steve Greene Jul 07 '15 at 13:02
  • Yes, seems like Database first can still be accomplished but as you mentioned without EDMX support.[Read article here](https://msdn.microsoft.com/en-us/magazine/dn890367.aspx) If not for this definitely something I will keep in mind for future projects. – Marius Vorster Jul 07 '15 at 13:37
  • Yes, my current project I did Code First with an existing database and the process was very similar. – Steve Greene Jul 07 '15 at 13:56

1 Answers1

0

Okay so I could not find a build in mechanism to do this so I had to use reflection to get all the entities in the DB context and then individually attempt to retrieve the FirstOrDefaut() in a try catch. It's not perfect but it will give a higher level of confidence in the alignment between EDMX and database.

public static List<Type> GetAllEntities()
{
    var entityList = new List<Type>();
    var context = typeof(<DatabaseContextClass>);

    foreach (var property in context.GetProperties())
    {
        if (!property.PropertyType.IsGenericType
            || property.PropertyType.GetGenericTypeDefinition() != typeof(ObjectSet<>)) // EF 4
            continue;

        var entityType = property.PropertyType.GetGenericArguements()[0];
        entityList.Add(entityType);

        return entityList;  
    }
}

public static T GetFirstObject<T>() where T : EntityObject
{
    var context = new <DatabaseContextClass>();

    IQueryable<T> = dbQuery = context.CreateObjectSet<T>();

    return dbQuery.AQsNoTracking().FirstOrDefault();
}
Marius Vorster
  • 196
  • 1
  • 10