簡體   English   中英

具有ApplicationUser屬性的實體,當我在服務中使用它時,該屬性為null

[英]Entity with ApplicationUser property, when i use it in a service the property is null

我有一個名為CarOwner的實體。 每個車主都有一個屬性ServiceAppUser AutoService。 當我添加新的CarOwner時,我在CarOwnerService中使用Create方法

在SSMS中,每件事看起來都不錯。

但是,當我點擊Details方法並嘗試通過GetById獲取CarOwner時,在CarOwnerService中,我發現了AutoService == null和Cars == null的CarOwner。

public class CarOwner : BaseModel<int>
{
    public CarOwner()
    {
        this.Cars = new HashSet<Car>();
    }

    public string Name { get; set; }

    public string Bulstat { get; set; }

    public string Address { get; set; }

    public string City { get; set; }

    public string MRP { get; set; }

    public string Email { get; set; }

    public string Telephone { get; set; }

    public virtual ICollection<Car> Cars { get; set; }

    public decimal Obligation { get; set; }

    public string ServiceAppUserId { get; set; }

    public virtual ServiceAppUser AutoService { get; set; }
}


public class CarOwnerService : ICarOwnerService
{
    private IRepository<CarOwner> carOwnerRepository;
    private IMapper mapper;

    public CarOwnerService(IRepository<CarOwner> carOwnerRepository, IMapper mapper)
    {
        this.carOwnerRepository = carOwnerRepository;
        this.mapper = mapper;
    }

    public async Task<int> Create(CarOwnerCreateViewModel model, ServiceAppUser user)
    {

        var carOwner = mapper.Map<CarOwner>(model);
        carOwner.AutoService = user;

        await this.carOwnerRepository.AddAsync(carOwner);
        await this.carOwnerRepository.SaveChangesAsync();

        var id = carOwner.Id;

        return id;
    }



    public CarOwner GetById(int id)
    {
        var allCarOwners = this.carOwnerRepository.All();
        var carOwner = allCarOwners.FirstOrDefault(o => o.Id == id);

        return carOwner;
    }

    public IEnumerable<CarOwnerViewModel> GetAll()
    {

        var all = this.carOwnerRepository.All().ToList().Select(co => mapper.Map<CarOwnerViewModel>(co));

        return all;
    }
}

我不確定,但是聽起來您可能正在使用Entity Framework Core或另一個ORM,因為您提到了SSMS。

沒有看到您的IRepository實現,很難確定,但是通常您描述的問題是因為您沒有告訴ORM包括對象圖的其他部分。

例如

dbContext.CarOwners
         .Include(c => c.Cars)
         .Include(c => c.AutoService)
         .Select(c => c);

將填寫CarsAutoService返回所有對象。

警告請小心,僅准確Include任何呼叫所需的內容,否則將導致加載太多數據並降低應用程序速度。

這是我的IRepository,是的,我正在使用EF Core

public interface IRepository<TEntity>
 where TEntity : class
{
    IQueryable<TEntity> All();

    bool Contains(TEntity entity);

    Task AddAsync(TEntity entity);

    void Delete(TEntity entity);

    Task<int> SaveChangesAsync();
}

這是我對IRepository的實現:

public class DbRepository<TEntity> : IRepository<TEntity>, IDisposable
    where TEntity : class
{
    private readonly ServiceAppContext context;
    private DbSet<TEntity> dbSet;

    public DbRepository(ServiceAppContext context)
    {
        this.context = context;
        this.dbSet = this.context.Set<TEntity>();
    }

    public Task AddAsync(TEntity entity)
    {
        return this.dbSet.AddAsync(entity);
    }

    public IQueryable<TEntity> All()
    {
        return this.dbSet;
    }

    public void Delete(TEntity entity)
    {
        this.dbSet.Remove(entity);
    }

    public Task<int> SaveChangesAsync()
    {
        return this.context.SaveChangesAsync();
    }

    public void Dispose()
    {
        this.context.Dispose();
    }

    public bool Contains(TEntity entity)
    {
        return this.dbSet.Contains(entity);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM