我是第一次使用ASP.NET Core,我正在try 创建一个API.我有一个问题,当我试图返回一个城市的用户使用懒惰加载代理程序包.有谁能帮帮我吗?谢谢你的帮助.
这是我的dbcontext
配置
services.AddDbContext<ApplicationDbContext>(option =>
{
option.UseLazyLoadingProxies()
.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
option.LogTo(Console.WriteLine,LogLevel.Information);
option.EnableSensitiveDataLogging();
});
这是我的User
个实体:
using ecommerce.Domain.Abstract;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using tables.Base.Entity;
namespace ecommerce.Domain.Entities
{
public class User : BaseEntity
{
public Guid Id { get; set; }
public Guid CityId { get; set; }
public virtual City City { get; set; }
public string Name { get; set; }
[Range(0,Double.MaxValue)]
public int Point { get; set; }
public Guid AccountId { get; set; }
public virtual Account Account { get; set; }
public bool IsBlocked { get; set; }
}
}
这是我对City
实体的配置:
using ecommerce.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ecommerce.infrutructure.Configuration
{
internal class CityConfigration : IEntityTypeConfiguration<City>
{
public void Configure(EntityTypeBuilder<City> builder)
{
builder.Property(c => c.Delivery_Price).HasDefaultValue(0);
builder.Property(c=>c.status).HasDefaultValue(false);
builder.HasMany(c => c.Users)
.WithOne(u => u.City);
}
}
}
在这里,user.city
在返回中为空
public async Task<OperationResultBase<UserWithToken>> Handle(AddUserCommand request, CancellationToken cancellationToken)
{
var Account = mapper.Map<Account>(request);
var Accountresult = await this.userManager.CreateAsync(Account, request.Password);
this.DbContext.SaveChanges();
if (!Accountresult.Succeeded)
{
throw new CanotCreateAccountException(Accountresult.Errors);
}
var User = mapper.Map<User>(request, opts => opts.AfterMap((src, desc) => desc.AccountId = Account.Id));
this.DbContext.Users.Add(User);
this.DbContext.SaveChanges();
TokenDto TokenInfo = jwtRepository.GetTokens(Account);
UserWithToken result = new UserWithToken()
{
Id = User.Id,
Name = User.Name,
UserName = User.Account.UserName,
City = User.City,
Email = User.Account.Email,
IsBlocked = User.IsBlocked,
Point = User.Point,
TokenInfo = TokenInfo
};
return Created<UserWithToken>(result, "The user was created successfully");
}