I have generic repository. There is a method Save() to persist entity in db. I want to check primary key through Table.GetPropertyValue(“ID”, entity) – how i should implement it?
private Table<T> Table
{
get { return db.GetTable<T>(); }
}
public void Save(T entity)
{
if (Table.GetPropertyValue("ID", entity) == 0)
{
Add(entity);
}
else if (Table.GetOriginalEntityState(entity) == null)
{
Table.Attach(entity);
Table.Context.Refresh(RefreshMode.KeepCurrentValues, entity);
}
db.SubmitChanges();
}
Say I have a class B defined as follows:
Then you could get the value of the id like this:
with the
GetIdmethod defined as follows:A more generic solution would be to make a method like this:
which would be called like so: