Hello everyone,
I have made a generic save function for EF:
public void Save(T entity)
{
using (C context = new C())
{
string entitySetName = context.FindEntitySetByEntity<T>();
T entityInDDBB = GetEntityByKey(entity, entitySetName, context);
//if we didn't find the entity in database make an insert, if not an update.
if (entityInDDBB == null)
{
**context.AddObject(entitySetName, entity);**
}
else
{
context.ApplyCurrentValues(entitySetName, entity);
}
context.SaveChanges();
}
}
The problem is that if we pass a derived type to AddObject (f.e: teacher) but the mapping expects Person it throws an error.
How could I change the type to the object (I suppose it is impossible without creating a new one) or do you know any other way to make it work?
Regards.