对于使用实体框架进行更新,我向DTO公开了具有所有属性的用户,但很少为属性分配值。
示例
public class Employee
{
public int Id {get;set;}
public string Name {get;set;}
public Datetime CreatedDateTime {get;set;}
public Datetime UpdatedDateTime {get;set;}
}
public class EmployeeDTO
{
public int Id {get;set;}
public string Name {get;set;}
[JsonIgnore]
public Datetime CreatedDateTime {get;set;}
[JsonIgnore]
public Datetime UpdatedDateTime {get;set;}
}
现在Dto已填充
EmployeeDto e = new EmployeeDto ()
{
Id =1,
Name = "xxxxxxxxx"
}
现在我们已经调用了更新方法
public bool Update(EmployeeDto e )
{
var repo = _dbset.Employee.Get(x => x.id == e.Id)
var d = _mapper.Map<Employee,EmployeeDto>(repo,e);
_dbset.Employee.UpdateAsync(d);
}
**,但在映射“创建日期”和“更新日期”时,将其设置为默认值,这给了我SQL异常**
在这种情况下,请帮助我。抱歉,如果我不清楚
答案 0 :(得分:1)
在ModelMappingProfile类中添加此行,它将忽略您将指定的属性。您还可以检查此链接以了解情况。 https://dotnettutorials.net/lesson/ignore-using-automapper-in-csharp/
SELECT * FROM YourTableName
WHERE
convert(DATETIME,tbl_Date) BETWEEN Convert(DATETIME,CASE WHEN isnull(@FromDate,'')='' THEN jobMain_JobDate ELSE isnull(@FromDate,'') END)
AND Convert(DATETIME, CASE WHEN isnull(@ToDate,'')='' THEN tbl_Date ELSE isnull(@ToDate,'') END)