Asp.Net 核心通用存储库模式软删除
Asp.Net Core Generic Repository Pattern Soft Delete
我正在尝试在我的 Repository
中创建一个 Soft Delete
操作,但我必须在不创建任何接口或 class 的情况下执行此操作。让我先告诉你我的方法,
public void Delete(T model)
{
if (model.GetType().GetProperty("IsDelete") == null )
{
T _model = model;
_model.GetType().GetProperty("IsDelete").SetValue(_model, true);//That's the point where i get the error
this.Update(_model);
}
else
{
_dbSet.Attach(model);
_dbSet.Remove(model);
}
}
我遇到了 Object reference not set to an instance of an object.
异常。我当然知道那是什么意思,但我就是想不通,也不知道该怎么做。不知道有没有更好的办法。
感谢阅读!
伙计们,你们真的要看看我哪里出错了。我正在编辑我的问题。
public abstract class Base
{
protected Base()
{
DataGuidID = Guid.NewGuid();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid DataGuidID { get; set; }
public int? CreatedUserId { get; set; }
public int? ModifiedUserId { get; set; }
public string CreatedUserType { get; set; }
public string ModifiedUserType { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
public bool? IsDelete { get; set; } //That's the property
}
每种类型的模型 class 都继承自 Base
class。当我创建一个新对象时,它采用空值。这就是为什么我将 属性 控制为
==null
.
w首先检查 属性 IsDelete
是否为空,然后尝试设置 属性 的值,显然为空。
if (model.GetType().GetProperty("IsDelete") == null )
应该是
if (model.GetType().GetProperty("IsDelete") != null )
编辑:
现在我们知道您想检查可为空的 bool 的值,我们必须采用另一种方法。
// first we get the property of the model.
var property = model.GetType().GetProperty("IsDelete");
// lets assume the property exists and is a nullable bool; get the value from the property.
var propertyValue = (bool?)property.GetValue(model);
// now check if the propertyValue not has a value.
if (!propertyValue.HasValue)
{
// set the value
property.SetValue(model, true);
...
}
我正在尝试在我的 Repository
中创建一个 Soft Delete
操作,但我必须在不创建任何接口或 class 的情况下执行此操作。让我先告诉你我的方法,
public void Delete(T model)
{
if (model.GetType().GetProperty("IsDelete") == null )
{
T _model = model;
_model.GetType().GetProperty("IsDelete").SetValue(_model, true);//That's the point where i get the error
this.Update(_model);
}
else
{
_dbSet.Attach(model);
_dbSet.Remove(model);
}
}
我遇到了 Object reference not set to an instance of an object.
异常。我当然知道那是什么意思,但我就是想不通,也不知道该怎么做。不知道有没有更好的办法。
感谢阅读!
伙计们,你们真的要看看我哪里出错了。我正在编辑我的问题。
public abstract class Base
{
protected Base()
{
DataGuidID = Guid.NewGuid();
}
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid DataGuidID { get; set; }
public int? CreatedUserId { get; set; }
public int? ModifiedUserId { get; set; }
public string CreatedUserType { get; set; }
public string ModifiedUserType { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? ModifiedDate { get; set; }
public bool? IsDelete { get; set; } //That's the property
}
每种类型的模型 class 都继承自 Base
class。当我创建一个新对象时,它采用空值。这就是为什么我将 属性 控制为
==null
.
w首先检查 属性 IsDelete
是否为空,然后尝试设置 属性 的值,显然为空。
if (model.GetType().GetProperty("IsDelete") == null )
应该是
if (model.GetType().GetProperty("IsDelete") != null )
编辑:
现在我们知道您想检查可为空的 bool 的值,我们必须采用另一种方法。
// first we get the property of the model.
var property = model.GetType().GetProperty("IsDelete");
// lets assume the property exists and is a nullable bool; get the value from the property.
var propertyValue = (bool?)property.GetValue(model);
// now check if the propertyValue not has a value.
if (!propertyValue.HasValue)
{
// set the value
property.SetValue(model, true);
...
}