C#中的LINQ-SQL:存储库模式的UpdateByID方法 [英] C# Linq-SQL: An UpdateByID method for the Repository Pattern
问题描述
我已经实现了一种库
类,它有具有 GetByID
, DeleteByID
等方法,但我有实施 UpdateByID
方法的麻烦。
I have implemented a sort of Repository
class and it has has GetByID
, DeleteByID
methods and so on, but I'm having trouble implementing the UpdateByID
method.
我做了这样的事情:
public virtual void UpdateByID(int id, T entity)
{
var dbcontext = DB;
var item = GetByID(dbcontext, id);
item = entity;
dbcontext.SubmitChanges();
}
protected MusicRepo_DBDataContext DB
{
get
{
return new MusicRepo_DBDataContext();
}
}
但它没有更新传递的实体。
But it's not updating the passed entity.
有没有人实现这样的方法?
Has anyone implemented such a method ?
的作为参考,这里是 GetByID
法的
[更新]
正如马克正确的建议,我只是改变了局部变量的值。那么你认为我应该如何去了解这种方法吗?使用反射和属性从实体
复制到项目
?
As Marc correctly suggested, I am merely changing the values of the local variable. So how do you think I should go about this method? Use reflection and copy the properties from entity
to item
?
推荐答案
您已经更新了所有的是一个局部变量;对于工作,你就必须给的成员值的从实体
复制到项目
- 。不是那么简单
All you have updated is a local variable; for that to work you would have to copy the member values from entity
to item
- not quite so simple.
类似下面;我使用的唯一原因 TKEY的
是我的Northwind.Customer测试,其中有一个字符串键;-p
Something like below; the only reason I used TKey
was that I tested on Northwind.Customer, which has a string key ;-p
使用元模型的优势在于,它即使您正在使用POCO类(和基于XML的映射),并且它不尝试更新任何无关的样板工程。
The advantage of using the meta-model is that it works even if you are using POCO classes (and the xml-based mapping), and it doesn't try to update anything unrelated to the model.
有关这个例子的目的,我已经通过了在数据方面,你需要在某个时候加入的SubmitChanges
,但。剩下的应该是直接的可比性。
For the purposes of the example, I have passed in the data-context, and you need to add a SubmitChanges
at some point, but the rest should be directly comparable.
顺便说一句 - 如果你很高兴从传入的对象采取的ID,这将是一件容易的事 - 然后你可以支持复合身份。表
BTW - if you are happy to take the ID from the passed in object, that would be easy too - and then you could support composite identity tables.
static void Update<TEntity>(DataContext dataContext, int id, TEntity obj)
where TEntity : class
{
Update<TEntity, int>(dataContext, id, obj);
}
static void Update<TEntity, TKey>(DataContext dataContext, TKey id, TEntity obj)
where TEntity : class
{
// get the row from the database using the meta-model
MetaType meta = dataContext.Mapping.GetTable(typeof(TEntity)).RowType;
if(meta.IdentityMembers.Count != 1) throw new InvalidOperationException("Composite identity not supported");
string idName = meta.IdentityMembers[0].Member.Name;
var param = Expression.Parameter(typeof(TEntity), "row");
var lambda = Expression.Lambda<Func<TEntity,bool>>(
Expression.Equal(
Expression.PropertyOrField(param, idName),
Expression.Constant(id, typeof(TKey))), param);
object dbRow = dataContext.GetTable<TEntity>().Single(lambda);
foreach (MetaDataMember member in meta.DataMembers)
{
// don't copy ID
if (member.IsPrimaryKey) continue; // removed: || member.IsVersion
// (perhaps exclude associations and timestamp/rowversion? too)
// if you get problems, try using StorageAccessor instead -
// this will typically skip validation, etc
member.MemberAccessor.SetBoxedValue(
ref dbRow, member.MemberAccessor.GetBoxedValue(obj));
}
// submit changes here?
}
这篇关于C#中的LINQ-SQL:存储库模式的UpdateByID方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!