locked
MVC noob with system.data.entity.core.entitycommandexecutionexception error RRS feed

  • Question

  • I have this super simple app. I am working through various training material on MVC and Entity Framework. Here is the code for 2 types of items:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    
    namespace HomeManager.Domain
    {
        public abstract class InvItem
        {
            public virtual  int Id { get; set; }
            public virtual DateTime Added { get; set; }
            [Required]
            [StringLength(50, ErrorMessage="Item name cannot exceed 50 characters.")]
            public virtual string Name { get; set; }
            public virtual string Size { get; set; }
            public virtual Location Location { get; set; }
    
            public InvItem()
            {
                // default non null dattetimevalues
                Added = DateTime.Now;
    
            }
        }
        
    
        public class HomeMadeItem : InvItem
        {
            public virtual string Instructions { get; set; }
            public virtual int Count { get; set; }
    
        }
        public class StoreBoughtItem : InvItem
        {
            public virtual string UPC { get; set; }
        }
    }
    

    As you can see from the InvItem, each item has a location:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel.DataAnnotations;
    
    
    namespace HomeManager.Domain
    {
        public class Location
        {
            // So I think the deal is validation done with data annotations to get free validation from MVC. 
            
            
            public virtual int Id { get; set; }
            [Required]
            [StringLength(50, ErrorMessage="The location name cannot be longer that 50 characters.")]
            public virtual string Name { get; set; }
            // TODO: What I would really like is a list of items
            // of either type without their specific properties.
            // concatonate the two lists in a different property? Figure it.
            public virtual ICollection<HomeMadeItem> HomeMadeItems { get; set;}
            public virtual ICollection<StoreBoughtItem> StoreBoughtItems { get; set;  }
            
    
        }
    }
    

    And finally, the database stuff:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    
    
    namespace HomeManager.Domain
    {
        public class InventoryDb : DbContext
    
        {
    
            public DbSet<HomeMadeItem> HomeMadeItems { get; set; }
            public DbSet<StoreBoughtItem> StoreBoughtItems { get; set; }
            public DbSet<Location> Locations { get; set; }
    
            // As I understand it, the config info of the co
            public InventoryDb()
                : base("DefaultConnection")
            {
    
            }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<HomeMadeItem>().Property(p => p.Name).IsRequired();
                modelBuilder.Entity<Location>().Property(p => p.Name).IsRequired();
    
            }
    
            
    
        }
    }
    

    On my main page, I want to list the items with the location name and the item name ordered by location. So my controller has

            private InventoryDb _db = new InventoryDb();
            public ActionResult Index()
            {
    
                var cooked = _db.HomeMadeItems;
                cooked.OrderBy(item => item.Location.Name);
                return View(cooked);
            }
    

    And finally, the View

    @model System.Data.Entity.DbSet<HomeManager.Domain.HomeMadeItem>
    
    @{
        ViewBag.Title = "View1";
    }
    
    <h2>Home</h2>
    
    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <table border="1">
        <tr>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
            @item.Name</td>
            <td>
            
    
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
                @Html.ActionLink("Details", "Details", new { id=item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id=item.Id })
            </td>
        </tr>
    }
    
    </table>
    

    The code seen here works great. But I cannot access the item's location. When I look at item in the watch window in debug mode, I see Value as

    '((HomeManager.Domain.InvItem)(item)).Location' threw an exception of type 'System.Data.Entity.Core.EntityCommandExecutionException'

    Anyone have any idea what I may have done wrong? There is something here I am not getting. Thanks.

    • Moved by Caillen Tuesday, December 31, 2013 9:52 AM
    Monday, December 30, 2013 6:47 PM

Answers

  • Hi StephanieVT,

    Please post ASP.NET questions in ASP.NET forums where you could get more help.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Just Karl Tuesday, December 31, 2013 8:55 PM
    • Marked as answer by StephanieVT Monday, January 6, 2014 1:46 PM
    Tuesday, December 31, 2013 9:51 AM

All replies

  • Hi StephanieVT,

    Please post ASP.NET questions in ASP.NET forums where you could get more help.

    Thanks for your understanding.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Just Karl Tuesday, December 31, 2013 8:55 PM
    • Marked as answer by StephanieVT Monday, January 6, 2014 1:46 PM
    Tuesday, December 31, 2013 9:51 AM
  • Specifically, this should be asked in the http://forums.asp.net/1146.aspx/1?MVC forum.

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog:http://unlockpowershell.wordpress.com
    My Book:Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C40686F746D61696C2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Tuesday, December 31, 2013 8:56 PM
  • Thank you. I had not seen that forum.
    Monday, January 6, 2014 1:45 PM