从更新中排除属性而不是更新整个对象

问题描述 投票:0回答:2

我使用ASP.NET Boilerplate与Code-First Entity Framework 6和MVC 5。

为了更新实体,我使用的是UpdateAsync

如何在执行更新之前从实体中排除某些属性?

我应该使用什么功能,这是在ASP.NET Boilerplate中实现的还是尚未实现?

我在实体框架6中实现了如下:

public virtual TEntity UpdateWithExcludeProperities(TEntity entity,string [] properities)
{
    if (entity == null)
        throw new ArgumentException("Paramter cannot be null", "entity");

    var existedEntity = SelectFromContext(entity.ID);
    if (existedEntity == null)
        throw new ObjectNotFoundException("Record is not found!");

    _context.Entry(existedEntity).CurrentValues.SetValues(entity);
    foreach (var name in properities)
    {
        _context.Entry(existedEntity).Property(name).IsModified = false;
    }
    _context.SaveChanges();
    return existedEntity;
}

public virtual TEntity SelectFromContext(Guid id)
{
    TEntity entity;
    entity = DbSet<TEntity>().SingleOrDefault(e => e.ID == id);
    return entity;
}

但有没有可能在ASP.NET Boilerplate中实现此代码?

c# asp.net-mvc-5 entity-framework-6 aspnetboilerplate change-tracking
2个回答
0
投票

你走错了路!使用指定的字段更新实体非常简单。

1-创建DTO。

2-配置映射

3-获取实体并将DTO映射到实体。

查看我的例子。在此示例中,Student实体具有3个属性。在StudentAppService UpdateOnlyNameOfStudent()方法中,我们仅更新学生的Name字段。请注意,我甚至没有运行_studentRepository.Update(student),因为AspNet Boilerplate在方法结束时提交更改(请参阅automatic save change

StudentDto.cs

[AutoMapFrom(typeof(Student))]
public class StudentDto: EntityDto<long>
{
     public string Name { get; set; }    
}

Student.cs

public class Student: Entity
{
     public string Name { get; set; }  

     public int SchoolNumber { get; set; }  

     public DateTime RegisterDate { get; set; }  
}

StudentAppService.cs

public class StudentAppService : IStudentAppService 
{
    private readonly IRepository<Student> _studentRepository;


    public RoleAppService(IRepository<Student> studentRepository)
    {
       _studentRepository = studentRepository;
    }

    public override async void UpdateOnlyNameOfStudent(StudentDto input)
    {
        var student = _studentRepository.Get(input.Id);
        ObjectMapper.Map(input, student);
    }

    /*
    public override async void UpdateOnlyNameOfStudent_Alternative(StudentDto input)
    {
        var student = _studentRepository.Get(input.Id);
        student.Name = input.Name;            
    }
    */
}

AspNet Boilerplate使用AutoMapper来映射对象。见Object To Object Mapping


0
投票

我能够在ASP.NET Boilerplate中通过直接使用Ef核心LINQ查询来实现这一点

public virtual TEntity SelectFromContext(TPrimaryKey id)
{
    TEntity entity;
    entity = Repository.GetDbContext().Set<TEntity>().SingleOrDefault(e => e.Id.Equals(id));
    return entity;
}

public virtual async Task<TEntity> UpdateWithExclude(TEntity entity, string[] properities)
{
        if (entity == null)
            throw new ArgumentException("Paramter cannot be null", "entity");

        var existedEntity = SelectFromContext(entity.Id);
        if (existedEntity == null)
            throw new ObjectNotFoundException("Record is not found!");

        var currentValues = Repository.GetDbContext().Entry(existedEntity).CurrentValues;

        foreach (var properteyName in currentValues.PropertyNames)//make default false value for all 
        {
            var y = Repository.GetDbContext().Entry(existedEntity).Property(properteyName);
            if (!properities.Contains(y.Name))
                Repository.GetDbContext().Entry(existedEntity).Property(properteyName).IsModified = false;
        }

        Repository.GetDbContext().Entry(existedEntity).CurrentValues.SetValues(entity);

        Repository.GetDbContext().SaveChanges();
        var UpdatedEntity = SelectFromContext(entity.Id);
        return await Task.FromResult(UpdatedEntity);
}
© www.soinside.com 2019 - 2024. All rights reserved.