简体   繁体   English

具有ApplicationUser属性的实体,当我在服务中使用它时,该属性为null

[英]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. 将填写CarsAutoService返回所有对象。

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.

相关问题 当我将数组作为ApplicationUser的属性时会发生什么? - What happens when I have an array as a property of ApplicationUser? 为什么我在ApplicationUser中的自定义对象属性为空? - Why is my Custom Object Property in ApplicationUser null? 使用UserName作为实体框架中的外键创建ApplicationUser导航属性 - Creating an ApplicationUser navigation property using UserName as the foreign key in Entity Framework ApplicationUser上的ASP.net Core 1.0映射属性返回null - ASP.net Core 1.0 Mapping property on ApplicationUser returning null 当我在模型类中使用导航属性时,Null Reference Exception - Null Reference Exception when i use navigation property in model class 在MVC 5中推荐的方法,以将Applicationuser用作不同DBContext中的Navigation属性 - Recommended approach in MVC 5 to use Applicationuser as Navigation property in different DBContext 添加不相关实体时,实体框架将属性值更改为null - Entity framework changes property value to null when adding unrelated entity 实体框架说更新实体时属性为null - Entity Framework says property is null when updating entity 对ApplicationUser属性的更改不会写入数据库 - Changes to ApplicationUser property are not written to the database 如果我第一次获得属性,实体框架将仅将相关实体属性设置为“null” - Entity Framework will only set related entity property to “null” if I first get the property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM