C#泛型类-如何替换值
我在C有点麻烦# 我有一个通用类来处理EntityFramework,如下所示:C#泛型类-如何替换值,c#,entity-framework,class,generics,C#,Entity Framework,Class,Generics,我在C有点麻烦# 我有一个通用类来处理EntityFramework,如下所示: public void update(TEntity entity) { try { using (db = new SystemEntities()) { db.Set<TEntity>().Attach(entity); db.Entry(entity).State = EntityState.Modi
public void update(TEntity entity)
{
try
{
using (db = new SystemEntities())
{
db.Set<TEntity>().Attach(entity);
db.Entry(entity).State = EntityState.Modified;
db.SaveChanges();
}
}
catch (Exception e)
{
if (e.GetType() == typeof(DbEntityValidationException))
{
DbEntityValidationException error = (DbEntityValidationException)e;
foreach (var validationErrors in error.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Log.write(String.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage));
}
}
}
Log.write(e);
}
}
但c#返回一个异常,表示实体中没有最后一次_更新的定义。我如何解决这个问题?试试这个:
PropertyInfo myproperty = typeof(TEntity).GetProperty("last_update");
if (myproperty != null)
{
myproperty.SetValue(entity, DateTime.Now);
}
PropertyInfo myproperty = typeof(TEntity).GetProperty("last_update");
if (myproperty != null)
myproperty.SetValue(myobj, DateTime.Now);
试试这个:
PropertyInfo myproperty = typeof(TEntity).GetProperty("last_update");
if (myproperty != null)
myproperty.SetValue(myobj, DateTime.Now);
试试这个:
PropertyInfo myproperty = typeof(TEntity).GetProperty("last_update");
if (myproperty != null)
myproperty.SetValue(myobj, DateTime.Now);
试试这个:
PropertyInfo myproperty = typeof(TEntity).GetProperty("last_update");
if (myproperty != null)
myproperty.SetValue(myobj, DateTime.Now);
我将在实体级别引入一个接口
public interface IAuditable
{
public DateTime LastUpdated { get; set; }
}
public class MyEntity : IAuditable
{
...
public DateTime LastUpdated { get; set; }
}
您可以在存储库中对其进行测试
var trackableObj = myobj as IAuditable;
if (trackableObj != null)
trackableObj.LastUpdated = DateTime.Now;
我将在实体级别引入一个接口
public interface IAuditable
{
public DateTime LastUpdated { get; set; }
}
public class MyEntity : IAuditable
{
...
public DateTime LastUpdated { get; set; }
}
您可以在存储库中对其进行测试
var trackableObj = myobj as IAuditable;
if (trackableObj != null)
trackableObj.LastUpdated = DateTime.Now;
我将在实体级别引入一个接口
public interface IAuditable
{
public DateTime LastUpdated { get; set; }
}
public class MyEntity : IAuditable
{
...
public DateTime LastUpdated { get; set; }
}
您可以在存储库中对其进行测试
var trackableObj = myobj as IAuditable;
if (trackableObj != null)
trackableObj.LastUpdated = DateTime.Now;
我将在实体级别引入一个接口
public interface IAuditable
{
public DateTime LastUpdated { get; set; }
}
public class MyEntity : IAuditable
{
...
public DateTime LastUpdated { get; set; }
}
您可以在存储库中对其进行测试
var trackableObj = myobj as IAuditable;
if (trackableObj != null)
trackableObj.LastUpdated = DateTime.Now;