I have a entity model as below:
public Entity
{
//Properties
}
public Activity : Entity
{
//Properties
public Action Action { get; set; }
public ICollection<Command> Commands { get; set; }
}
public Action : Entity
{
//Properties
}
public Command : Entity
{
//Properties
}
I have a store procedure that returns List of Activities along with Related Actions and Commands of each Activity. How I can map the result sets of the before-mentioned store procedure in EF5 or EF 6 Codefirst ? I already used ObjectContext.Translate
method of which is explained in MSDN article Stored Procedures with Multiple Result Sets .
But the problem is all my entities are derived from Entity
class and that's why when I used ObjectContext.Translate
for Activity
.. I can't use it for Action
and Command
since It maps the EntitySetName
of the Entity
class for Activity
so if I use it for either Action
or Command
it'll raise error. How I can manage that?
Stored Procedure with multiple result set is very much possible in EF5 or greater. Here is the link for detailed answer
This works well as an extention on your IDataReader
public static List<T> MapTo<T>(this IDataReader dr)
{
var list = new List<T>();
while (dr.Read())
{
var obj = Activator.CreateInstance<T>();
foreach (var prop in obj.GetType().GetProperties())
{
if (!Equals(dr[prop.Name], DBNull.Value))
{
prop.SetValue(obj, dr[prop.Name], null);
}
}
list.Add(obj);
}
return list;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.