public void AddDto(HardwareDto hardwareDto) { Hardware hardwareDomain = new Hardware(); hardwareDomain.ID = hardwareDto.ID; hardwareDomain.Name = hardwareDto.Name; hardwareDomain.Number = hardwareDto.Number; hardwareDomain.CreateTime = hardwareDto.CreateTime; hardwareDomain.CreateUser = hardwareDto.CreateUser; //转换工程名为工程id var projects = from r in _dataContext.Set <Project>() where r.Name == hardwareDto.Project select r; foreach (var project in projects) { hardwareDomain.ProjectID = project.ID; } //添加到表hardware _hardwareRepository.AddEntity(hardwareDomain); //查出hardware获取id号 int id = 0; var hardwares = from r in _dataContext.Set <Hardware>() where r.Name == hardwareDto.Name select r; foreach (var hardware in hardwares) { id = hardware.ID; } //添加items到表itembaseonhardware for (int i = 0; i < hardwareDto.Items.Count; i++) { ItemBaseOnHardware itemBaseOnHardware = new ItemBaseOnHardware(); itemBaseOnHardware.Name = hardwareDto.Items[i]; itemBaseOnHardware.HardwareID = id; _itemBaseOnHardwareRepository.AddEntity(itemBaseOnHardware); } }