提问人:B.Balamanigandan 提问时间:1/6/2017 最后编辑:shA.tB.Balamanigandan 更新时间:1/14/2017 访问量:2018
List<Task> - 使用 C# 实体框架的 UPSERT 数据库记录
List<Task> - UPSERT database record using C# Entity Framework
问:
我有一个对象,我正在尝试使用单个数据库实体上下文使用多个任务(并行执行)更新记录(即更新/删除)。但是我得到以下异常Employee
Message =“对象引用未设置为对象的实例。
请考虑以下 DTO
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<ContactPhone> ContactPhoneNumbers { get; set; }
public List<ContactEmail> ContactEmailAddress { get; set; }
}
public class ContactPhone
{
public int ContactId { get; set; }
public string Type { get; set; }
public string Number { get; set; }
}
public class ContactEmail
{
public int ContactId { get; set; }
public string Type { get; set; }
public string Number { get; set; }
}
员工表:
EmployeeId FirstName LastName
_________________________________
1 Bala Manigandan
ContactPhone 表:
ContactId EmployeeId Type Number
__________________________________________
1 1 Fax 9123456789
2 1 Mobile 9123456789
ContactPhone 表:
ContactId EmployeeId Type EmailAddress
______________________________________________
1 1 Private [email protected]
2 1 Public [email protected]
传入 API 对象是
DTO.Employee emp = new DTO.Employee()
{
EmployeeId = 1,
FirstName = "Bala",
LastName = "Manigandan",
ContactPhoneNumbers = new List<DTO.ContactPhone>
{
new DTO.ContactPhone()
{
Type = "Mobile",
Number = "9000012345"
}
},
ContactEmailAddress = new List<DTO.ContactEmail>()
{
new DTO.ContactEmail()
{
Type = "Private",
EmailAddress = "[email protected]"
},
new DTO.ContactEmail()
{
Type = "Public",
EmailAddress = "[email protected]"
}
}
};
我收到一个 API 请求,要求更新手机号码并删除指定员工的传真号码。
考虑以下任务方法:
public void ProcessEmployee(DTO.Employee employee)
{
if(employee != null)
{
DevDBEntities dbContext = new DevDBEntities();
DbContextTransaction dbTransaction = dbContext.Database.BeginTransaction();
List<Task> taskList = new List<Task>();
List<bool> transactionStatus = new List<bool>();
try
{
Employee emp = dbContext.Employees.FirstOrDefault(m => m.EmployeeId == employee.EmployeeId);
if (emp != null)
{
Task task1 = Task.Factory.StartNew(() =>
{
bool flag = UpdateContactPhone(emp.EmployeeId, employee.ContactPhoneNumbers.FirstOrDefault().Type, employee.ContactPhoneNumbers.FirstOrDefault().Number, dbContext).Result;
transactionStatus.Add(flag);
});
taskList.Add(task1);
Task task2 = Task.Factory.StartNew(() =>
{
bool flag = RemoveContactPhone(emp.EmployeeId, "Fax", dbContext).Result;
transactionStatus.Add(flag);
});
taskList.Add(task2);
}
if(taskList.Any())
{
Task.WaitAll(taskList.ToArray());
}
}
catch
{
dbTransaction.Rollback();
}
finally
{
if(transactionStatus.Any(m => !m))
{
dbTransaction.Rollback();
}
else
{
dbTransaction.Commit();
}
dbTransaction.Dispose();
dbContext.Dispose();
}
}
}
public async Task<bool> UpdateContactPhone(int empId, string type, string newPhone, DevDBEntities dbContext)
{
bool flag = false;
try
{
var empPhone = dbContext.ContactPhones.FirstOrDefault(m => (m.EmployeeId == empId) && (m.Type == type));
if (empPhone != null)
{
empPhone.Number = newPhone;
await dbContext.SaveChangesAsync();
flag = true;
}
}
catch (Exception ex)
{
throw ex;
}
return flag;
}
public async Task<bool> RemoveContactPhone(int empId, string type, DevDBEntities dbContext)
{
bool flag = false;
try
{
var empPhone = dbContext.ContactPhones.FirstOrDefault(m => (m.EmployeeId == empId) && (m.Type == type));
if (empPhone != null)
{
dbContext.ContactPhones.Remove(empPhone);
await dbContext.SaveChangesAsync();
flag = true;
}
}
catch (Exception ex)
{
throw ex;
}
return flag;
}
我收到以下异常:
Message =“对象引用未设置为对象的实例。
在这里,我附上了屏幕截图供您参考
我的要求是并行执行所有数据库进程,请帮助我如何使用 Task 毫无例外地实现这一目标UPSERT
答:
可能发生此错误的地方是 -employee.ContactPhoneNumbers.FirstOrDefault().Type, employee.ContactPhoneNumbers.FirstOrDefault()
可能是空的,因为您没有急于加载它,也没有将属性标记为它会延迟加载。employee.ContactPhoneNumbers
virtual
因此,要解决此问题:
1.将导航属性标记为延迟加载virtual
public virtual List<ContactPhone> ContactPhoneNumbers { get; set; }
public virtual List<ContactEmail> ContactEmailAddress { get; set; }
- 或者 Eager 加载实体
.Include
- 或显式加载实体
dbContext.Entry(emp).Collection(s => s.ContactPhoneNumbers).Load();
dbContext.Entry(emp).Collection(s => s.ContactEmailAddress ).Load();
评论
virtual
public partial class DevDBEntities : DbContext
Employee
virtual
public virtual ICollection<ContactPhone> ContactPhones { get; set; }
public partial class Employee
employee.ContactPhoneNumbers
1st)停止在不同线程中使用上下文。
DbContext 不是线程安全的,仅此一项就会导致许多奇怪的问题,甚至是疯狂的 NullReference 异常
现在,你确定你的并行代码比非并行实现更快吗?
我非常怀疑这一点。
据我所知,您甚至没有更改您的 Employee 对象,所以我不明白为什么要加载它(两次)
我认为您所需要的只是
1) 加载您需要更新并设置新号码
的手机 2)删除未使用的手机
不必加载此记录。只需使用默认构造函数并设置 Id。
EF 可以处理其余的(当然需要附加新创建的对象)
3)保存更改
(使用相同的上下文执行 1,2,3 合 1 方法)
如果出于某种原因,您决定执行多项任务
- 在每个任务中创建一个新上下文
- 将代码包装在 TransactionScope 中
更新
我刚刚注意到这一点:
catch (Exception ex) { throw ex; }
这很糟糕(你丢失了堆栈跟踪)
删除 try/catch 或使用
catch (Exception ex) { throw ; }
更新 2:
一些示例代码(我假设您的输入包含要更新/删除的实体的 ID)
var toUpdate= ctx.ContactPhones.Find(YourIdToUpdate);
toUpdate.Number = newPhone;
var toDelete= new ContactPhone{ Id = 1 };
ctx.ContactPhones.Attach(toDelete);
ctx.ContactPhones.Remove(toDelete);
ctx.SaveChanges();
如果您采用并行方法
using(TransactionScope tran = new TransactionScope()) {
//Create and Wait both Tasks(Each task should create it own context)
tran.Complete();
}
评论
下一个:为什么我在循环时收到错误消息
评论
employee.ContactPhoneNumbers.FirstOrDefault().Type, employee.ContactPhoneNumbers.FirstOrDefault()