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.
I did something like this:
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 ?
For reference, here is the GetByID
method
[Update]
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
toitem
- not quite so simple.Something like below; the only reason I used
TKey
was that I tested on Northwind.Customer, which has a string key ;-pThe 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.
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.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.