简体   繁体   中英

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

I have an entity called CarOwner. Every car owner has a property ServiceAppUser AutoService. When i add a new CarOwner i use Create method in CarOwnerService

In the SSMS every thing looks fine.

But when i hit the Details method and try to get a CarOwner by GetById, in the CarOwnerService I found a CarOwner with AutoService == null, and a collection of Cars == null

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;
    }
}

I'm not sure, but it sounds like you might be using Entity Framework Core or another ORM, because you mentioned SSMS.

Without seeing your IRepository implementation it is difficult to be certain, but generally the problem you describe is because you didn't tell the ORM to include the other parts of your object graph.

For example

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

Would return all of your objects, with Cars and AutoService filled in.

WARNING Be careful to only Include exactly what you need for any call, or you will end up loading way too much data and slowing down your application.

this is my IRepository, and yes i am using 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();
}

And this is my implementaion of 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM