0

我无法为每个客户将我的借出物品链接到我的图书馆。当它通过“AddToLibrary”方法时它做得很好,但是在检索它时,medialibrary 是空的,并且查询IEnumerable<Item> ItemsOnLoan method返回 null。这是一个非常基本的 ASP.NET MVC 4 应用程序,我对此很陌生,所以我可能错过了一些愚蠢的东西。

我只想能够将一个项目添加到外借项目表中,让每个客户的外借项目列表出现在他们的个人库中(在模型中定义),然后检索他们的项目列表。以下是所有代码,我使用的是代码优先方法。谢谢 :)

模型

public class Customer
{
        public int Id { get; set; }
        public string ForeName { get; set; }
        public string SurName { get; set; }
        public Address address { get; set; }
        public string Email { get; set; }
        public string Telephone { get; set; }
        public string Mobile { get; set; }
        public List<LoanedItem> Library { get; set; }

        public Customer()
        {

            if (Library == null || Library.Count == 0)
            {
                Library = new List<LoanedItem>();
            }
        }

        public IEnumerable<Item> ItemsOnLoan
        {
            get
            {
                var items = (from i in Library
                             where i.Customer.Id == this.Id
                             select i).OfType<item>();
                return items;
            }
        }
}

外借物品型号

public class LoanedItem
{
    public int? Id { get; set; }
    public Customer Customer { get; set; }
    public MediaItem Item { get; set; }
}

ItemController --> 添加到库方法

public ActionResult AddToLibrary(int id)
{

    Item libraryItem = db.Items.Find(id);
    Customer c = db.Customers.Find(1);
    LoanedItem newLoanGame = new LoanedItem()
    {
        Customer = c,
        Item = libraryItem
    };
    db.LoanedItems.Add(newLoanGame);
    db.SaveChanges();
    return RedirectToAction("Index");
}

客户控制器

public ActionResult ViewProfile(int id = 1)
{
    Customer c = db.Customers.Find(id);
    if (c == null)
    {
        return HttpNotFound();
    }
    return View(c);
}
public ActionResult GetLibraryItems(int id = 1)
{
    var items = db.Customers.Find(id).ItemsOnLoan;
    return View(items);
}

语境

public class LibraryContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<LoanedItem> LoanedItems { get; set; }
    public DbSet<Item> Items { get; set; }

    public LibraryContext()
        : base("LbContext")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new CustomerConfiguration());
        modelBuilder.Configurations.Add(new LoanedItemConfiguration());
        modelBuilder.Entity<Item>();
        modelBuilder.Entity<Address>();

        base.OnModelCreating(modelBuilder);
    }
    }
4

1 回答 1

0

假设启用了代理生成,请尝试以下操作:

public class Customer
{
    public int Id { get; set; }
    public string ForeName { get; set; }
    public string SurName { get; set; }
    public Address address { get; set; }
    public string Email { get; set; }
    public string Telephone { get; set; }
    public string Mobile { get; set; }
    public virtual ICollection<LoanedItem> ItemsOnLoan { get; set; }

    public Customer()
    {
    }
}

使用它来访问:

public ActionResult GetLibraryItems(int id = 1)
{
    var customer = db.Customers.Find(id);
    if (customer != null)
    {
        var items = customer.ItemsOnLoan;
        return View(items);
    }
    //handle not found or throw an exception
    throw new Exception();
}

单击此链接以获取有关代理和延迟加载的更多信息。

于 2013-04-16T09:45:58.937 回答