locked
EntityType 'GigViewModel' has no key defined. Define the key for this EntityType. So why doesn't it work when I include one? RRS feed

  • Question

  • User-618475340 posted

    Using this viewmodel

    public class GigViewModel
        {        
            [Display(Name = "Band")]
            public string bandname { get; set; }
            [Display(Name = "Venue")]
            public string venuename { get; set; }
            [Display(Name = "Address")]
            public string address1 { get; set; }
            [Display(Name = "City")]
            public string city { get; set; }
            [Display(Name = "Date")]
            public DateTime whatdate { get; set; }
            [Display(Name = "Starts at")]
            public TimeSpan starttime { get; set; }
        }

    and this in the controller

            public ActionResult Gigs()
            {            
                var yourgigs = (from g in dbg.gigs
                               from v in dbg.Venues
                               from b in dbg.Bands
                               from ga in g.gigsaccasses
                               where (v.venueid == g.venueid &&
                               b.bandid == g.bandid)
    
                               select new GigViewModel()
                               {
                                   bandname = b.bandname,
                                   venuename = v.venuename,
                                   address1 = v.address1,
                                   city = v.city,
                                   whatdate = g.whatdate,
                                   starttime = g.starttime
                               });                           
                return View(yourgigs);

    it's supposed to display the fields in the view model.  However it tells me

    EntityType 'GigViewModel' has no key defined. Define the key for this EntityType

    So I add a key to the viewmodel and so had this -

    public class GigViewModel
        {
            [Key]
            public int gigid { get; set; }
            [Display(Name = "Band")]
            public string bandname { get; set; }
            [Display(Name = "Venue")]
            public string venuename { get; set; }
            [Display(Name = "Address")]
            public string address1 { get; set; }
            [Display(Name = "City")]
            public string city { get; set; }
            [Display(Name = "Date")]
            public DateTime whatdate { get; set; }
            [Display(Name = "Starts at")]
            public TimeSpan starttime { get; set; }
            
        }

    It then throws this error

    The entity or complex type 'OnStageTonight_MVC.Models2.GigViewModel' cannot be constructed in a LINQ to Entities query.

    What's causing this?

    Tuesday, June 2, 2015 11:30 AM

Answers

  • User1577371250 posted

    Hi,

    Change this and try

      public ActionResult Gigs()
            {            
                var yourgigs = (from g in dbg.gigs
                               from v in dbg.Venues
                               from b in dbg.Bands
                               from ga in g.gigsaccasses
                               where (v.venueid == g.venueid &&
                               b.bandid == g.bandid)
                               select new 
                               {
                                   bandname = b.bandname,
                                   venuename = v.venuename,
                                   address1 = v.address1,
                                   city = v.city,
                                   whatdate = g.whatdate,
                                   starttime = g.starttime
    
                               }).ToList().Select(x => new GigViewModel()
                                {
    
                                   bandname = x.bandname,
                                   venuename = x.venuename,
                                   address1 = x.address1,
                                   city = x.city,
                                   whatdate = x.whatdate,
                                   starttime = x.starttime
    
                                }); 
    
                              
                return View(yourgigs);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 3, 2015 7:15 AM