【问题标题】:EF5 ASP.NET MVC 4 Linq query not returning anything & model property null -> Code FirstEF5 ASP.NET MVC 4 Linq 查询不返回任何内容和模型属性 null -> 代码优先
【发布时间】:2013-04-16 09:20:00
【问题描述】:

我无法为每个客户将我借出的物品链接到我的图书馆。当它通过“AddToLibrary”方法时它做得很好,但是在检索它时,媒体库是空的,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);
    }
    }

【问题讨论】:

    标签: asp.net-mvc entity-framework code-first datamapper


    【解决方案1】:

    假设启用了代理生成,试试这个:

    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();
    }
    

    关注this link 了解有关代理和延迟加载的更多信息。

    【讨论】:

    • 异常是:值不能为空。参数名称:查询上的来源 var items = (from i in Library where i.Customer.Id == this.Id select i).OfType();
    • @user1290653 上面的代码中没有定义Game 类?
    • 抱歉,那是另一回事,我更改了评论
    • 摆脱我上面做的方法。将ItemsOnLoan 更改为virtual ICollection&lt;LoanedItem&gt;
    • 对,它似乎在工作,图书馆基本上正在保存,太棒了,非常感谢。我知道这将是一件小事!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多