![](/img/trans.png)
[英]What happens when I have an array as a property of ApplicationUser?
[英]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);
将填写Cars
和AutoService
返回所有对象。
警告请小心,仅准确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.