Я добавил пакет Microsoft.EntityFrameworkCore.Proxies и добавил UseLazyLoadingProxies() в свой DbContext
Код: Выделить всё
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseLazyLoadingProxies().UseSqlServer("Server = SRV84; Database = BASE_DEMO1; User Id = sa; Password = Ab@123456");
}
}
Код: Выделить всё
[HttpPost]
public async Task 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(userInfo);
#endregion
#region Create new Group
if (oUserCreate.GroupIds.Count > 0)
{
foreach (var item in oUserCreate.GroupIds)
{
if (item > 0)
{
await BaseService.CreateAsync(new GroupUserCreateDto() { GroupId = item, UserId = idUser });
}
}
}
#endregion
return Ok();
}
Код: Выделить всё
[HttpGet]
public async Task GetUsers([FromQuery] UserGridPagingDto pagingModel)
{
var predicates = pagingModel.GetPredicates();
var result = await BaseService.FilterPagedAsync(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);
}
Я пытаюсь удалить свою базу данных, комментарий мою строку подключения и перезагрузите компьютер. Но я все равно смог получить данные, которые добавил ранее. Вот что я вижу в терминале.
Код: Выделить всё
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
Подробнее здесь: https://stackoverflow.com/questions/784 ... -loading-w