Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 泛型类来初始化我的几个审核属性_C#_Entity Framework_Entity Framework 6_Entity Framework Core - Fatal编程技术网

C# 泛型类来初始化我的几个审核属性

C# 泛型类来初始化我的几个审核属性,c#,entity-framework,entity-framework-6,entity-framework-core,C#,Entity Framework,Entity Framework 6,Entity Framework Core,我必须时不时地在m方法中映射一些审计属性,因此,我编写了下面的通用方法 private static T MapAuditFields<T>(long id , bool isNew ) where T : new() { dynamic result = new T(); if (isNew) { result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime(); result.xx

我必须时不时地在m方法中映射一些审计属性,因此,我编写了下面的通用方法

private static T MapAuditFields<T>(long id , bool isNew ) where T : new()
{
    dynamic result = new T();
    if (isNew)
    {
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xx= id;
        result.xx= id;
    }
    else
    {
        result.xxxxx= id;
        result.xxxxxx= DateTimeHelper.GetCurrentUTCDateTime();
    }
    return (T)result;
}
private static T mapaAuditFields(long id,bool isNew),其中T:new()
{
动态结果=新的T();
如果(是新的)
{
result.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime();
result.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime();
结果xx=id;
结果xx=id;
}
其他的
{
结果xxxxx=id;
result.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime();
}
返回(T)结果;
}
但每一个例子都给了我一个新的对象。因此,当使用我的efcore自动映射器进行映射时,我做了两次映射

有人能帮我避免在这里映射吗

例:

public bool保存(请求)
{
var ob=mapaAuditFields(3,true);//获取审核字段//创建新的请求对象
_mapper.Map(请求);//将请求主体与实体映射
}

提前感谢您的投入。抱歉,如果我不清楚

MapAuditFields方法是否可以通过反射实现。还请记住,
id
xxxxxx
属性必须存在于对象类型
T

private static T MapAuditFields<T>(T requestObj, long id , bool isNew )
{

    if (isNew)
    {
        typeof(T).GetProperty("id").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());
        typeof(T).GetProperty("xxxxx").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());

        ...
    }
    else
    {
        typeof(T).GetProperty("id").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());
        typeof(T).GetProperty("xxxxxx").SetValue(request, DateTimeHelper.GetCurrentUTCDateTime());
    }

    return (T)result;
}
private static T mapaAuditFields(T requestObj,long id,bool isNew)
{
如果(是新的)
{
typeof(T).GetProperty(“id”).SetValue(请求,DateTimeHelper.GetCurrentUTCDateTime());
typeof(T).GetProperty(“xxxxx”).SetValue(请求,DateTimeHelper.GetCurrentUTCDateTime());
...
}
其他的
{
typeof(T).GetProperty(“id”).SetValue(请求,DateTimeHelper.GetCurrentUTCDateTime());
typeof(T).GetProperty(“xxxxxx”).SetValue(请求,DateTimeHelper.GetCurrentUTCDateTime());
}
返回(T)结果;
}
我建议你使用基本对象。那么,

private static T MapAuditFields<T>(T requestObj, long id , bool isNew ) where T : MyBaseObject
{

    if (isNew)
    {
        requestObj.id = id;
        requestObj.xxxxx = DateTimeHelper.GetCurrentUTCDateTime()

......
private static T mapaAuditFields(T requestObj,long id,bool isNew),其中T:MyBaseObject
{
如果(是新的)
{
requestObj.id=id;
requestObj.xxxxx=DateTimeHelper.GetCurrentUTCDateTime()
......
您还可以使用Automapper进行映射

public class SetTimestampMappingAction: IMappingAction<object, MyBaseObject>
{
    public SetTraceIdentifierAction(/*if you are using dependency injection use it here*/)
    {

    }

    public void Process(SomeModel source, SomeOtherModel destination, ResolutionContext context)
    {
        destination.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime()

    }
}

public class SomeProfile : Profile
{
    public SomeProfile()
    {
        CreateMap<object, MyBaseObject>()
            .AfterMap<SetTimestampMappingAction>();
    }
}
public类SetTimestampMappingAction:IMappingAction
{
public SetTraceIdentifierAction(/*如果使用依赖项注入,请在此处使用它*/)
{
}
公共作废流程(某些模型源、某些其他模型目标、ResolutionContext上下文)
{
destination.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime()
}
}
公共类SomeProfile:Profile
{
公共档案()
{
CreateMap()
.AfterMap();
}
}

传递要映射审核字段的对象已作为参数传递以避免新实例解决了我的问题。这将返回相同的对象并填充值

private static T MapAuditFields<T>(long id , bool isNew, T t ) where T : new()
{
    dynamic result = t;
    if (isNew)
    {
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xx= id;
        result.xx= id;
    }
    else
    {
        result.xxxxx= id;
        result.xxxxxx= DateTimeHelper.GetCurrentUTCDateTime();
    }
    return (T)result;
}
private static T mapaAuditFields(long id,bool isNew,T),其中T:new()
{
动态结果=t;
如果(是新的)
{
result.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime();
result.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime();
结果xx=id;
结果xx=id;
}
其他的
{
结果xxxxx=id;
result.xxxxxx=DateTimeHelper.GetCurrentUTCDateTime();
}
返回(T)结果;
}

您没有在第一个方法中进行映射,而是在创建新实例。您的原始对象在哪里?我的原始对象是请求主体,由于上述通用方法,我被迫进行两次映射
private static T MapAuditFields<T>(long id , bool isNew, T t ) where T : new()
{
    dynamic result = t;
    if (isNew)
    {
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xxxxxx = DateTimeHelper.GetCurrentUTCDateTime();
        result.xx= id;
        result.xx= id;
    }
    else
    {
        result.xxxxx= id;
        result.xxxxxx= DateTimeHelper.GetCurrentUTCDateTime();
    }
    return (T)result;
}