How to update entity from new created detached entity
开发者_JAVA百科Let's say I created a new product like this:
Product p=new Product(){ Id= 2,Name="some name"};
My product variable has never been attached to a data context, how can I attach this entity so that my existing product in the database with Id=2 gets update with the name of my detached product?
I hope something similar would work
 Datacontext db = new Datacontext(); // replace with your DataContext
 Product originalProduct = 
         db.Products.Single(p => p.Id == 2); // get product with Id 2
 originalProduct.Name = "SomeName";  // only reset Name prop
 originalProduct = p;                // or you may assign p to originalProduct
 db.SubmitChanges();                 // submit changes to DB
A bit easier solution would be to implement the ICloneable interface on the entity you would like to work like that. Then, all you need to do is call Clone() into 'original' entity you pulled from the DB.
Example :
class Product : ICloneable
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public object Clone()
    {
        return new Product() { Id=this.Id, Name=this.Name };
    }
}
And then all you need to do is :
Datacontext db = new Datacontext(); // replace with your DataContext
Product originalProduct = 
        db.Products.Single(p => p.Id == 2); // get product with Id 2
db.originalProduct = p.Clone()
db.SubmitChanges();
Edit :
I ran into the same problem at work, so far the most elegant solution I found was to build an extension method that recieves the newly created entity, in your case Product, and copies it's properties (except for the Identifier) to the entity you pulled from the DataContext.
I copied all the properties via Reflection, this way, if I update the entity, then my extension method will still work.
Hope this helps you too.
If you find a more elegant solution to this problem, I would like to hear :)
Just to add on to gillyb's post and the comment I left there. A very easy way for cloning your entity is to do this:
Private Function CloneEntity(Of TEntity)(ByVal entity As TEntity) As TEntity
    Dim dataContext As New DataContext()
    Dim entityType As System.Type = GetType(TEntity)
    ' If the purpose of the clone is just to attach it the existing entity to a new
    ' DataContext, you can use IdentityMembers in place of PersistantDataMembers. Setting
    ' only the IdentityMembers is enough to allow for attaching the clone as an "originaL"
    ' entity to the DataContext.
    Dim dataMembers = dataContext.Mapping.GetTable(entityType).RowType.PersistentDataMembers
    Dim clone As TEntity ' Do something here to create an entity of the desired type. 
    Dim boxedClone As Object = clone
    For Each dm In dataMembers
         ' Depending on how your ColumnAttribute is set, you would use StorageAccessor if
         ' the Storage property is set. Otherwise, you would use MemberAccessor instead.
          dm.StorageAccessor.SetBoxedValue(boxedClone, dm.StorageAccessor.GetBoxedValue(entity))
    Next
    Return clone
End Function
Then you would just need to do this:
Dim dataContext As New DataContext()
dataContext.GetTable(Of Object)().Attach(entityToUpdate, CloneEntity(entityToUpdate))
dataContext.SubmitChanges()
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论