public void UpdateDto(HardwareDto hardwareDto) { Hardware hardware = new Hardware(); hardware.ID = hardwareDto.ID;//这里的ID必须给值,不然更新不到数据库 hardware.Name = hardwareDto.Name; hardware.Number = hardwareDto.Number; hardware.CreateTime = hardwareDto.CreateTime; hardware.CreateUser = hardwareDto.CreateUser; //转换工程名为id var projects = from r in _dataContext.Set <Project>() where r.Name == hardwareDto.Project select r; foreach (var project in projects) { hardware.ProjectID = project.ID; } _hardwareRepository.UpdateEntity(hardware); }