I’m using EF Core 6.0 lazy loading
I have added the package Microsoft.EntityFrameworkCore.Proxies And I have added UseLazyLoadingProxies() in my DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseLazyLoadingProxies().UseSqlServer("Server = SRV84; Database = BASE_DEMO1; User Id = sa; Password = Ab@123456");
}
}
This is my API:
[HttpPost]
public async Task<IActionResult> CreateUser(UserCreateDto oUserCreate) //UserCreateDto oUserCreate
{
var oUser = await _authorizationService.GetUserByUserName(oUserCreate.Username);
if (oUser.Id > 0)
{
return BadRequest("Username already exists");
}
oUserCreate.Status = (int)StatusUser.Deactive;
oUserCreate.CreatedDate = DateTime.Now;
oUserCreate.ModifiedDate = DateTime.Now;
oUserCreate.Status = (int)StatusUser.Deactive;
var idUser = await _authorizationService.CreateUser(oUserCreate);
if (idUser == 0)
{
await AddLogAsync(UserIdentity.FullName + ": create " + oUserCreate.FullName, OBJECT.USERS, (int)ActionLogs.Add, (int)StatusLogs.Error);
return BadRequest("There was an error!");
}
await AddLogAsync(UserIdentity.FullName + ": create " + oUserCreate.FullName, OBJECT.USERS, (int)ActionLogs.Add, (int)StatusLogs.Success);
#region Create new Userinfo
UserInfoCreateDto userInfo = new UserInfoCreateDto();
userInfo.Address = oUserCreate.Address;
userInfo.Email = oUserCreate.Email;
userInfo.Avatar = oUserCreate.Avatar;
userInfo.Phone = oUserCreate.PhoneNumber;
userInfo.Sex = oUserCreate.Sex;
userInfo.UserId = idUser;
await BaseService.CreateAsync<UserDetail, UserInfoCreateDto>(userInfo);
#endregion
#region Create new Group
if (oUserCreate.GroupIds.Count > 0)
{
foreach (var item in oUserCreate.GroupIds)
{
if (item > 0)
{
await BaseService.CreateAsync<GroupUser, GroupUserCreateDto>(new GroupUserCreateDto() { GroupId = item, UserId = idUser });
}
}
}
#endregion
return Ok();
}
[HttpGet]
public async Task<IActionResult> GetUsers([FromQuery] UserGridPagingDto pagingModel)
{
var predicates = pagingModel.GetPredicates();
var result = await BaseService.FilterPagedAsync<User, UserGridDto>(pagingModel, predicates.ToArray());
if (pagingModel.GroupId > 0)
{
if (result.Data.Count > 0)
{
for (int i = 0; i < result.Data.Count; i++)
{
result.Data[i].IsCheck = result.Data[i].LstGroupIds.Contains(pagingModel.GroupId);
}
}
}
return Ok(result);
}
When I post ‘User’ from API, my database cannot update, but I could get data.
I try to delete my database ,comment my connection string & restart my computer.But I still could get data that I added before. This is what I see in the terminal.
16' with options: using lazy loading proxies
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (52ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT COUNT(*)
FROM [Users] AS [u]
WHERE [u].[DeletedUserId] IS NULL
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
Executed DbCommand (52ms) [Parameters=[@__p_0='?' (DbType = Int32)], CommandType='Text', CommandTimeout='30']
SELECT [t].[Id], [t].[UserName], [t].[FullName], [t].[c], [t].[Status], [t].[CreatedDate], [t].[DeletedUserId], [t].[c0], [g].[GroupId], [g].[Id]
FROM (
SELECT TOP(@__p_0) [u].[Id], [u].[UserName], [u].[FullName], (
SELECT TOP(1) [u0].[Email]
FROM [UserDetail] AS [u0]
WHERE [u].[Id] = [u0].[UserId]) AS [c], [u].[Status], [u].[CreatedDate], [u].[DeletedUserId], (
SELECT TOP(1) [u1].[Phone]
FROM [UserDetail] AS [u1]
WHERE [u].[Id] = [u1].[UserId]) AS [c0]
FROM [Users] AS [u]
WHERE [u].[DeletedUserId] IS NULL
ORDER BY [u].[Id] DESC
) AS [t]
LEFT JOIN [GroupUser] AS [g] ON [t].[Id] = [g].[UserId]
ORDER BY [t].[Id] DESC
What should I do now
New contributor
Quang Hưng Nguyễn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.