實體框架7訪問ICollection中的實體對象

asp.net-core-mvc c# entity-framework-core

我有兩個實體用戶和地址。地址是Users實體上的ICollection屬性。但是我無法訪問ICollection中的各個地址。這是可能的,我只是有錯誤的語法或實體框架不允許這樣做。

代碼:

用戶:

 public partial class User
{
    public User()
    {
        Addresses = new HashSet<Address>();
        Comments = new HashSet<Comment>();
        Orders = new HashSet<Order>();
        Posts = new HashSet<Post>();
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual ICollection<Address> Addresses { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual ICollection<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

地址:

public partial class Address
{
    public long Id { get; set; }
    public bool Active { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public DateTime Created { get; set; }
    public string State { get; set; }
    public DateTime? Updated { get; set; }
    public long? UserId { get; set; }
    public string Zip { get; set; }

    public virtual User User { get; set; }
}

存儲庫(父):

public class Repository<T> where T : class
{
    private DemoContext _context;

    protected DbSet<T> DbSet;

    public Repository(DemoContext context)
    {
        _context =context;
        DbSet = _context.Set<T>();
    }

    public virtual T Get(int Id)
    {
        // TODO: Implement with return of DbSet.Find();

        // DbSet.Find(Id);
        return null;
    }

    public virtual List<T> GetAll()
    {
        return DbSet.ToList();
    }

    public virtual void Add(T entity)
    {
        DbSet.Add(entity);
    }

    public virtual void Update(T user)
    {
        _context.Entry<T>(user)
            .State = EntityState.Modified;
    }

    public virtual void SaveChanges()
    {
        _context.SaveChanges();
    }

    public virtual void Delete(int Id)
    {
        // TODO: Implement with return of DbSet.Find();

        // DbSet.Remove(Dbset.Find(Id));
    }

UserRepository:

 public class UserRepository : Repository<User>
{
    public UserRepository(DemoContext context)
        : base(context)
    {

    }

    public override User Get(int Id)
    {
        return DbSet
            .Where(o => o.Id == Id)
            .Include(o => o.Orders)
            .Include(o => o.Addresses)
            .ToList()
            .Single();
    }

    public override List<User> GetAll()
    {
        return DbSet
            .Include(o => o.Orders)
            .Include(o => o.Addresses)
            .ToList();
    }

    public override void Delete(int Id)
    {     
        DbSet.Remove(Get(Id));
    }
}

以下代碼不允許我訪問用戶實體的ICollection屬性中的第一個地址

[HttpGet("[action]")]
    public IActionResult Create()
    {
        var user = userRepository.Get(1);

        var order = new Order
        {
            Address = user.Addresses[0].Address,
            City = user.Addresses[0].City,
            State = user.Addresses[0].State,
            Zip = user.Addresses[0].Zip,
            User = user,
            SubTotal = 100,
            Tax = 25,
            Total = 125
        };

        orderRepository.Add(order);
        orderRepository.SaveChanges();

        return RedirectToAction("Index");
    }

請告知如何更正我的代碼,以便我可以訪問集合中的實體。

一般承認的答案

萬一有人想知道我是如何解決這個問題的答案:

正如COLD TOLD提到的ICollection不支持像索引這樣的數組。但是列表會將其更改為List而不是ICollection並刪除在custructor中創建的hashset

 public partial class User
{
    public User()
    {
    }

    public long Id { get; set; }
    public bool Active { get; set; }
    public DateTime? BirthDate { get; set; }
    public DateTime Created { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Password { get; set; }
    public DateTime? Updated { get; set; }
    public string Username { get; set; }
    public long? UserTypeId { get; set; }

    public virtual List<Address> Addresses { get; set; }
    public virtual List<Comment> Comments { get; set; }
    public virtual List<Order> Orders { get; set; }
    public virtual List<Post> Posts { get; set; }
    public virtual UserType UserType { get; set; }
}

這允許我訪問這些嵌套實體,如下所示:

[HttpGet("/")]
public IActionResult Create()
{
    var user = userRepository.Get(1);

    var order = new Order
    {
        Address = user.Addresses[0].Address1,
        City = user.Addresses[0].City,
        State = user.Addresses[0].State,
        Zip = user.Addresses[0].Zip,
        User = user,
        SubTotal = 100,
        Tax = 25,
        Total = 125
    };

    orderRepository.Add(order);
    orderRepository.SaveChanges();

    return RedirectToAction("Index");
}

熱門答案

我認為在您的選擇中包含許多包含可能會導致您的性能出現問題並不是一個好主意,您可能會嘗試更改查詢的順序

 return DbSet.Include(o => o.Orders)
            .Include(o => o.Addresses)
            .Where(o => o.Id == Id)
            .FirstOrDefault();


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow