【问题标题】:Entity Framework 6 Partial Stored Procedure MappingEntity Framework 6 部分存储过程映射
【发布时间】:2014-10-28 05:37:21
【问题描述】:

例如,我有一个这样的类:

public class Employee
{
   public int Id;
   public string Name;
   public double Salary;
   public DateTime BoD; 
}

以及只返回员工的idsalary 列的存储过程,我想要使用实体框架(6 或5),如下所示:

执行存储过程,并将其结果映射到Employee 对象,而不使用复杂类型。

请帮帮我。

注意:

  1. 请考虑程序会更复杂,我不想使用 linq 并返回 name 或 Bod。
  2. 这在我的项目中非常重要。
  3. 在 nhibernate 中您可以这样做。

【问题讨论】:

  • 你需要创建一个扩展函数
  • 怎么样??,我对此一无所知

标签: c# .net sql-server-2008 entity-framework-5 entity-framework-6


【解决方案1】:

这是你需要的:

// Create Two Classes

public class Employee
{
   public int Id;
   public string Name;
   public double Salary;
   public DateTime BoD; 
}

public class EmployeeIDAndSalary
{
   public int Id;
   public double Salary;
}

// Create Extension Function
// We need to use Automapper extension library. 
// Install Automapper using Nuget Package Manager. And create a class and add this code in it.

namespace Models.Mapper
{
    public static class EmployeeMapper
    {
        public static Employee MapToEmployee(this EmployeeIDAndSalary obj)
        { 
            if (obj != null)
            {
                Employee model = AutoMapper.TryAutoMap<Employee>(obj);                
                return model ;
            }
            return new Employee();
        }

        public static IEnumerable<Employee> MapToEmployee(this IEnumerable<EmployeeIDAndSalary> obj)
        {
            if (obj != null)
            {
                return obj.Select(x => x.ConvertToEmployee()).AsEnumerable();
            }
            return new List<Models.Employee>();
        }
    }

    public static class AutoMapper
    {
        static AutoMapper()
        {

        }

        public static T TryAutoMap<T>(object fromObject)
        {
            try
            {
                var currentObj = Activator.CreateInstance<T>();
                var type = currentObj.GetType();
                var propInfo = type.GetProperties();
                var _type = fromObject.GetType();
                foreach (var item in _type.GetProperties())
                {
                    var prop = propInfo.Where(c => c.Name == item.Name).FirstOrDefault();
                    if (prop != null)
                    {
                       // if (prop.GetType().IsAssignableFrom(item.GetType()))
                        {

                            if (IsGenericEnumerable(item.PropertyType))
                            {
                                continue;
                            }

                            if (prop.GetType() == item.GetType())
                            {
                                try
                                {
                                    prop.SetValue(currentObj, item.GetValue(fromObject, null), null);
                                }
                                catch { }
                            }
                        }
                    }
                }

                return currentObj;
            }
            catch
            {
                return default(T);
            }
        }

        private static bool IsClass(Type type)
        {            
            return type.IsClass && !type.IsPrimitive;
        }

        private static bool IsGenericEnumerable(Type type)
        {
            if (type.FullName.ToLower().Contains("datamodel") || type.FullName.ToLower().Contains("models") || type.FullName.ToLower().Contains("collection"))
            {
                return true;
            }
            return false;
        }
    }
}

// Use Namespace -> Models.Mapper.EmployeeMapper;
// Execute your procedure using EmployeeIdAndSalary
try
{
    var _objList = context.SqlQuery<EmployeeIDAndSalary>("YourProcedureName @SpParameterName", param).ToList().MapToEmployee();
}
catch
{ }

// I think this is all what you need.

【讨论】:

    猜你喜欢
    • 2011-07-14
    • 1970-01-01
    • 2017-12-31
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多