如何在Entry Framework中跟踪记录已成功创建.NET Core 2.0

时间:2018-02-07 18:41:20

标签: entity-framework .net-core-2.0

我正在研究.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();

0 个答案:

没有答案