我正在研究.NET CORE 2.0实体框架的应用程序。
我有以下代码在数据库中插入新记录,这是具有特定角色的新用户,工作正常,但我需要知道如何监控该记录是否已成功创建?和进入状态
bool recordCreated = false;
try
{
UserDataModel userWithDefaultRoleObj = new UserDataModel();
userWithDefaultRoleObj = new UserDataModel
{
Id = initializeNewUserWithRole.UserId,
Name = initializeNewUserWithRole.Name,
Surname = initializeNewUserWithRole.Surname,
Email = initializeNewUserWithRole.Email,
Roles = new List<UserRoleDataModel>
{
new UserRoleDataModel {
Role = Context.Roles.Where(roleName => roleName.Name == initializeNewUserWithRole.RoleName).First(),
User = userWithDefaultRoleObj
}
}
};
//var addRecordState = Context.Add<UserDataModel>(userWithDefaultRoleObj).State = Microsoft.EntityFrameworkCore.EntityState.Added;
var addRecordState = Context.Add<UserDataModel>(userWithDefaultRoleObj);
var x = Context.SaveChanges();