【发布时间】:2021-07-25 20:12:21
【问题描述】:
我试图在我的Repository 中创建一个Soft Delete 操作,但我必须在不创建任何接口或类的情况下这样做。让我先告诉你我的方法,
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
}
每种类型的模型类都继承自 Base 类。当我创建一个新对象时,它采用空值。这就是为什么我控制该属性为
==null.
【问题讨论】:
-
使用
!= null。 -
它没有任何意义,else 块总是工作,因为每个数据都有一个空值
IsDeleted属性 -
这意味着给定类型上没有这样的属性。范你发布完整的复制者,包括类型本身?
-
我编辑了我的问题。
标签: c# asp.net asp.net-core entity-framework-core asp.net-core-mvc