zoukankan      html  css  js  c++  java
  • Copy Entity Record

    public static T CopyEntity<T>(NorthwindEntities ctx, T entity, bool copyKeys = false) where T : EntityObject
            {
                T clone = ctx.CreateObject<T>();
                PropertyInfo[] pis = entity.GetType().GetProperties();
    
                foreach (PropertyInfo pi in pis)
                {
                    EdmScalarPropertyAttribute[] attrs = (EdmScalarPropertyAttribute[])pi.GetCustomAttributes(typeof(EdmScalarPropertyAttribute), false);
    
                    foreach (EdmScalarPropertyAttribute attr in attrs)
                    {
                        if (!copyKeys && attr.EntityKeyProperty)
                            continue;
    
                        pi.SetValue(clone, pi.GetValue(entity, null), null);
                    }
                }
    
                return clone;
            }

    How to using ?

    private void btnCopyNew_Click(object sender, EventArgs e)
            {
                Customer cust = customerBindingSource.Current as Customer;
                if (cust != null)
                {
                    //context.Detach(cust);
                    Customer customerCopy = CopyEntity<Customer>(context,cust);
                    Random rd = new Random();
                    string id = rd.Next(10000, 99999).ToString();
                    customerCopy.CustomerID = id;
    
                    context.Customers.AddObject(customerCopy);
                    //context.ObjectStateManager.ChangeObjectState(cust, EntityState.Added);
                    
                    //context.Refresh(System.Data.Objects.RefreshMode.StoreWins, cust);
                    context.SaveChanges();
    
                    //customerBindingSource.DataSource = context.Customers.Execute(System.Data.Objects.MergeOption.AppendOnly);
                    customerBindingSource.ResetBindings(false);
    
                    gridView1.FocusedRowHandle = gridView1.LocateByValue("CustomerID", id);
    
                    
                }
            }
  • 相关阅读:
    php使用PHPMailer邮件类发送邮件
    apache一个IP一个端口对应多个域名
    网页宽度自动适应手机屏幕宽度的方法
    PHP抓取网页图片
    innodb存储引擎
    mysql存储引擎概述
    mysql事务
    mysql字符集
    mysql数据对象
    SQL-基础学习4--聚集函数:AVG(),COUNT(),MAX(),MIN(),SUM();聚集不同值:DISTINCT
  • 原文地址:https://www.cnblogs.com/perock/p/3640797.html
Copyright © 2011-2022 走看看