.net MVC 4 problem with model.

Discussion in 'Site Programming, Development and Design' started by DiceBox, Apr 14, 2013.

  1. Code:
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    
    //my model
    public class Roll
    {
        [Key]
        public uint Id { get; set; }
        public long RandomSeed { get; set; }
        public string Expression { get; set; }
        public DateTime DateCreated { get; set; }
        public long Total { get; set; }
    
    }
    
    //my context
    public class DiceboxContext : DbContext
    {
        public DbSet<Roll> Rolls { get; set; }
    }
    
    Code:
    //my controller 
    
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace dicebox.Controllers
    {
        public class RollController : Controller
        {
            private DiceboxContext db = new DiceboxContext();
    
            //
            // GET: /Roll/
    
            public ActionResult Index()
            {
                return View(db.Rolls.ToList());
            }
    
            //
            // GET: /Roll/Details/5
    
            public ActionResult Details(int id = 0)
            {
                Roll roll = db.Rolls.Find(id);
                if (roll == null)
                {
                    return HttpNotFound();
                }
                return View(roll);
            }
    
            //
            // GET: /Roll/Create
    
            public ActionResult Create()
            {
                return View();
            }
    
            //
            // POST: /Roll/Create
    
            [HttpPost]
            public ActionResult Create(Roll roll)
            {
                if (ModelState.IsValid)
                {
                    db.Rolls.Add(roll);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
    
                return View(roll);
            }
    
            //
            // GET: /Roll/Edit/5
    
            public ActionResult Edit(int id = 0)
            {
                Roll roll = db.Rolls.Find(id);
                if (roll == null)
                {
                    return HttpNotFound();
                }
                return View(roll);
            }
    
            //
            // POST: /Roll/Edit/5
    
            [HttpPost]
            public ActionResult Edit(Roll roll)
            {
                if (ModelState.IsValid)
                {
                    db.Entry(roll).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                return View(roll);
            }
    
            //
            // GET: /Roll/Delete/5
    
            public ActionResult Delete(int id = 0)
            {
                Roll roll = db.Rolls.Find(id);
                if (roll == null)
                {
                    return HttpNotFound();
                }
                return View(roll);
            }
    
            //
            // POST: /Roll/Delete/5
    
            [HttpPost, ActionName("Delete")]
            public ActionResult DeleteConfirmed(int id)
            {
                Roll roll = db.Rolls.Find(id);
                db.Rolls.Remove(roll);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            protected override void Dispose(bool disposing)
            {
                db.Dispose();
                base.Dispose(disposing);
            }
        }
    }
    
    Most of this is boilerplate automatically generated code. Any time I hit the Any action except for the get /Roll/Create Action, it blows up with the following error message:

    \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Roll' has no key defined. Define the key for this EntityType.
    \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Rolls' is based on type 'Roll' that has no keys defined.

    But as you can already see, there is a key defined. There is also a key defined for the database table "Rolls" that backs this model.

    What am I doing wrong?
     
  2. I've discovered what "I" was doing wrong. Microsoft MVC does not allow uint as a primary key field. Why? I do not know.

    Also note for people using Visual Studio 2010 express... When creating controllers linked to a MySql bound context, you need to temporarilly change the providerName of the connectionString in your web.config file to "System.Data.SqlClient" and then back to "MySql.Data.MySqlClient" when you run the server.
     
  3. ComputerMan

    ComputerMan Winhost Staff

    Oh cool thanks for posting what you've discovered. I'm sure it will help someone else who has this type of problem.
     

Share This Page