Les vues sont-elles mappées dans EF?
Habituellement, je lance un test de configuration EF qui vérifie toutes les tables.
Je poste également le code principal mais cela ne fonctionne que si la clé primaire contient une et une seule propriété nommée Id. Vous pouvez le résoudre en appelant la méthode FirstOrDefault (vous pouvez jeter un oeil ici comment l'appeler How do I use reflection to call a generic method?).
[TestMethod]
public void All()
{
var properties = typeof (Context).GetProperties().Where(p => IsSubclassOfRawGeneric(typeof(DbSet<>), p.PropertyType));
foreach (PropertyInfo property in properties)
{
Type entityType = property.PropertyType.GetGenericArguments()[0];
PropertyInfo idProperty = entityType.GetProperty("Id");
if (idProperty == null)
{
Console.WriteLine("Id property not found. Cannot check type configuration");
continue;
}
Type idPropertyType = idProperty.PropertyType;
DbSet dbSet = _context.Set(entityType);
if (idPropertyType == typeof(string))
{
try
{
dbSet.Find("A");
}
catch (Exception e)
{
throw new Exception("Cannot access to DbSet " + property.Name, e);
}
}
else if (idPropertyType == typeof (int))
{
try
{
dbSet.Find(1);
}
catch (Exception e)
{
throw new Exception("Cannot access to DbSet " + property.Name, e);
}
}
else
{
Console.WriteLine("Id property type not supported ('{0}'). Cannot check type configuration", idPropertyType.Name);
continue;
}
}
}
static bool IsSubclassOfRawGeneric(Type generic, Type toCheck)
{
while (toCheck != null && toCheck != typeof(object))
{
var cur = toCheck.IsGenericType ? toCheck.GetGenericTypeDefinition() : toCheck;
if (generic == cur)
{
return true;
}
toCheck = toCheck.BaseType;
}
return false;
}