I used EF fluent API for mapping my POCO classes to database tables. I need to use the mapping configuration in my own application logic. More specifically let say I have entity "Product" and I have the following mapping for this entity:
public class ProductMap : EntityTypeConfiguration<Product>
{
public ProductMap()
{
....
this.HasKey(t => t.ProductId);
...
}
}
In my application logic I want to write a generic method which gets the key field of my entity (here ProductId). If I had used the data annotation attributes I could find my key property using reflection. My question is that is it basically possible achive the same when I define my mappings with fluent API?
Thanks diaho. I exactly came to the solution that you suggest but still it is not a good solution since entity framework does not allow us to make query using a field which is not pure entity field. Take the following example: I want to write a generic method which return an entity based on its id. I came to the following solution:
public TModel GetById<TModel, TKey>(TKey id) where TModel : ModelBase<TKey>
{
return _context.Set<TModel>().where(e => e.Id == id).FirstOrDefault();
}
This code however doesnt work since entity framework complains that e.Id
is not a field of drived entity class (its a field of parent class). Of course its possible to hack this issue by converting Set<TModel>()
to list (as follow) to force executing query earlier. But this is terrible in terms of performance since it load all entities to memory to retrieve a single one.
public TModel GetById<TModel, TKey>(TKey id) where TModel : ModelBase<TKey>
{
return _context.Set<TModel>().ToList().where(e => e.Id == id).FirstOrDefault();
}
Do you have better suggestion for this scenario?