[英]Entity with ApplicationUser property, when i use it in a service the property is null
I have an entity called CarOwner. 我有一个名为CarOwner的实体。 Every car owner has a property ServiceAppUser AutoService.
每个车主都有一个属性ServiceAppUser AutoService。 When i add a new CarOwner i use Create method in CarOwnerService
当我添加新的CarOwner时,我在CarOwnerService中使用Create方法
In the SSMS every thing looks fine. 在SSMS中,每件事看起来都不错。
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 但是,当我点击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;
}
}
I'm not sure, but it sounds like you might be using Entity Framework Core or another ORM, because you mentioned SSMS. 我不确定,但是听起来您可能正在使用Entity Framework Core或另一个ORM,因为您提到了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. 没有看到您的
IRepository
实现,很难确定,但是通常您描述的问题是因为您没有告诉ORM包括对象图的其他部分。
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. 将填写
Cars
和AutoService
返回所有对象。
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. 警告请小心,仅准确
Include
任何呼叫所需的内容,否则将导致加载太多数据并降低应用程序速度。
this is my IRepository, and yes i am using EF Core 这是我的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();
}
And this is my implementaion of IRepository: 这是我对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.