locked
How to resolve "One or more validation errors were detected during model generation"? RRS feed

  • Question

  • User-600123075 posted

    Hi, While i am trying the below code, i get error mentioning that "mvcdemo.Models.parentmodel: : EntityType 'parentmodel' has no key defined. Define the key for this EntityType.
    pmodel: EntityType: EntitySet 'pmodel' is based on type 'parentmodel' that has no keys defined".

    Note: I tried by using [Key] attribute in both parent and Test model.But No Luck.

    Anyone help me to get resolved this one?

    Is there any relationship between primarykey and foreign key of groupid? [groupid is primary key in creategroup class and foreign key in shift class]

    public ActionResult Test1()
    {
    //List<parentshift> psh = new List<parentshift>();

    List<CreateGroup> allgroup = new List<CreateGroup>();
    List<dynamicshift> allshift = new List<dynamicshift>();
    using (portalconnectionstring ps = new portalconnectionstring())
    {
    allgroup = ps.grp.OrderBy(m => m.groupid).ToList(); [the mentioned error appear at this place while tracing the code]
    allshift = ps.ds.OrderBy(m => m.shift).ToList();
    }
    ViewBag.populategroup = new SelectList(allgroup, "groupid", "groupname");
    ViewBag.populateshift = new SelectList(allshift, "shift", "shift");
    return View();
    }
    [HttpPost,ActionName("Test1")]
    public ActionResult Test2(FormCollection formCollection)
    {
    List<CreateGroup> allgroup = new List<CreateGroup>();
    List<dynamicshift> allshift = new List<dynamicshift>();
    using (portalconnectionstring ps = new portalconnectionstring())
    {
    allgroup = ps.grp.OrderBy(m => m.groupname).ToList();
    allshift = ps.ds.OrderBy(m => m.shift).ToList();
    }
    ViewBag.populategroup = new SelectList(allgroup, "groupid", "groupname");
    ViewBag.populateshift = new SelectList(allshift, "shift", "shift");
    createtable();
    }

    public void createtable()
    {
    //below is part of code
    mvcdemo.Models.ViewModels.Testmodel model = new mvcdemo.Models.ViewModels.Testmodel();

    model.hdNoOfColumns= count.ToString();
    dsCount = ds.Tables[0].Rows.Count;
    model.hdNoOfRows= dsCount.ToString();
    shiftCount = dsS.Tables[0].Rows.Count;
    }

    namespace mvcdemo.Models
    [Table("tblgroup", Schema = "public")]
    public class CreateGroup
    {


    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public virtual int groupid { get; set; }
    public string groupname { get; set; }
    }
    namespace mvcdemo.Models
    [Table("tblshift", Schema = "public")]
    public class dynamicsubject
    {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]

    public string groupid { get; set; }
    public string shift { get; set; }
    }


    namespace mvcdemo.Models
    [Table("shiftdetails",Schema="public")]
    public class Shift
    {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]


    public int groupid { get; set; }

    public string shift { get; set; }
    }


    namespace mvcdemo.Models.ViewModels
    public class Test (where Test is ViewModel)
    {

    public string hdNoOfColumns { get; set; }
    public string hdNoOfRows { get; set; }

    }

    To use two model in a single view, i have created ViewModels folder under Models folder and put both Shift model and Test model in that folder.
    Below is parentmodel which consists of shift and test model


    namespace mvcdemo.Models.ViewModels
    {
    public class parentmodel (ViewModel)
    {


    public Testmodel test { get; set; }

    public Shift shift { get; set; }

    }
    }


    View:
    @model mvcdemo.Models.ViewModels.parentmodel
    @using (Html.BeginForm("Test2","ShiftAlloc",FormMethod.Post,new {id="form1"}))

    {
    @Html.AntiForgeryToken()
    @Html.DropDownListFor(Model => Model.shift.groupid, @ViewBag.populategroup as SelectList, "select group", new { id = "DropDownList3" })
    @Html.DropDownListFor(m=>m.shift.shift,@ViewBag.populateshift as SelectList,"select shift")
    @Html.HiddenFor(m=>m.test.hdNoOfColumns,new {id="hdNoOfColumns"})
    @Html.HiddenFor(m => m.test.hdNoOfRows, new { id = "hdNoOfRows" })
    <input type="submit" value="Submit">
    }

    Friday, May 22, 2015 3:30 AM

Answers

  • User-271186128 posted

    Hi Sir,

    Is there any relationship between primarykey and foreign key of groupid? [groupid is primary key in creategroup class and foreign key in shift class]

    From your code and description, I suppose you want to configure the one to many relations. If that is the case, please refer to the following code:

        public class Student
        {
            public Student() { }
    
            public int StudentId { get; set; }
            public string StudentName { get; set; }
    
             public int StdandardRefId { get; set; }
            
            [ForeignKey("StandardRefId")]
            public virtual Standard Standard { get; set; }
        }
           
        public class Standard
        {
            public Standard()
            {
                StudentsList = new List<Student>();
            }
            public int StandardId { get; set; }
            public string Description { get; set; }
    
            public virtual ICollection<Student> Students { get; set; }
        }

    For more details, please refer the following articles:

    https://msdn.microsoft.com/en-us/data/jj591583

    http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 25, 2015 1:41 AM