zoukankan      html  css  js  c++  java
  • 一个用于 Entity Framework 对象拷贝的方法

    此方法用于两个不同类型Entity Framework 对象相同名称属性之间的拷贝

    public class EntityHelper

    {

    /// <summary>

    ///","号分隔忽略属性

    /// </summary>

    /// <param name="source">源对象</param>

    /// <param name="target">目标对象</param>

    /// <param name="ignorePoperties"></param>

    /// <returns></returns>

    public static TTarget EntityCopy<TSource, TTarget>(TSource source, TTarget target, string ignorePoperties)

    {

     

    List<string> ignoreP = new List<string>();

    if (!string.IsNullOrEmpty(ignorePoperties))

    {

    ignoreP = ignorePoperties.ToLower().Split(',').ToList();

    }

     

    ignoreP.Add("entitykey");

    ignoreP.Add("entitystate");

    var tFields = target.GetType().GetProperties();

    var sFields = source.GetType().GetProperties();

     

    foreach (var item in tFields)

    {

    if (!ignoreP.Contains(item.Name.ToLower()))

    {

    foreach (var si in sFields)

    {

    if (si.Name == item.Name)

    {

    object svalue = si.GetValue(source, null);

    object tvalue = item.GetValue(target, null);

    if (svalue != null && !svalue.Equals(tvalue))

    {

    item.SetValue(target, svalue, null);

    }

    }

    }

    }

    }

    return target;

     

    }

     

    /// <summary>

    ///","号分隔忽略属性

    /// </summary>

    /// <param name="source">源对象</param>

    /// <param name="target">目标对象</param>

    /// <returns></returns>

    public static TTarget EntityCopy<TSource, TTarget>(TSource source, TTarget target)

    {

    return EntityCopy(source, target, "");

     

    }

    调用:

    /// <summary>

    ///新上下级关系

    /// </summary>

    public int GetChagedPositionRelations()

    {

    var query = from pr in EISEEntities.View_Interface_PositionReport

    join p in EISEEntities.View_Interface_Position on pr.PositionId equals p.PositionId

    join dic in EISEEntities.View_Interface_Dictionary on p.DivisionID equals dic.DictionaryId

    where DivtionFilt.IndexOf(dic.Code) != -1

    && pr.LastModifiedOn > StartTime

    && pr.LastModifiedOn < EndTime

    select new { pr, p.PositionCode };

    var list = query.ToList();

    foreach (var item in list)

    {

    ocm_map_PositionRelation_Approval model = new ocm_map_PositionRelation_Approval();

    //

    EntityHelper.EntityCopy(item.pr, model);

    model.HRPositionCode = item.PositionCode;

    if (item.pr.ParentPositionId != null)

    {

    model.ParentPositionCode = (from pp in EISEEntities.View_Interface_Position

    where pp.PositionId == item.pr.ParentPositionId

    select pp.PositionCode).FirstOrDefault();

    }

    model.PositionRelationApprovalId = Guid.NewGuid();

    model.ApprovalStatus = (int)EISSyncApprovalStatus.Stored;

    SDMEntities.ocm_map_PositionRelation_Approval.AddObject(model);

    }

    SDMEntities.SaveChanges(SaveOptions.AcceptAllChangesAfterSave);

    return list.Count;

    }

    表结构:

  • 相关阅读:
    电商概念
    Linux知识点(二)
    linux知识点
    笔记8月20日
    考勤运行提示‘Length of values (115) does not match length of index (116) >>> ’
    数据透视表+数据条
    CCRC软件开发评审-材料应该怎么准备
    python os.walk函数
    httprunner 断言报错 expect_value 和check_value类型不一致
    自动化-Yaml文件读取函数封装
  • 原文地址:https://www.cnblogs.com/Qbit/p/2501053.html
Copyright © 2011-2022 走看看