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);
    
                    
                }
            }
  • 相关阅读:
    解决Spring+Quartz无法自动注入bean问题
    MacBook PRO蓝牙无法搜索设备
    解决HP打印机错误:Couldn't open fifo
    SQL调优
    ALTER SEQUENCE 修改序列解决唯一约束冲突 unique constraint violated
    Linux Unix 环境变量设置实例
    DB2解除锁表
    ExtJS远程数据-本地分页
    安装mysql
    celery使用
  • 原文地址:https://www.cnblogs.com/perock/p/3640797.html
Copyright © 2011-2022 走看看