You must Sign In to post a response.
  • Category: ASP.Net MVC

    Validation error in model

    Hi friends.

    I have created a mvc application, in which I took three class files in a model, one class file contains teams list and other class file contains the players list.
    The third class file I took as a DbContext class using the entity framework.
    Now I have a Teamcontroller and PlayersController.
    My requiremet is like below.
    1.I should display the teams list first(Teams view as a action link).
    2.When we click on the action link of a particular team it should take to the players view, where all players related to that team has to be displayed.
    Am facing the following error message while working on it.

    One or more validation errors were detected during model generation:

    \tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Players' has no key defined. Define the key for this EntityType.
    \tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'players' is based on type 'Players' that has no keys defined.
  • #764031
    Hi,

    I don't have knowledge in Entity Framework, but as per error I will guide you, teams & players should be in primarykey and foreignkey relationship. I guess you missed to implement that while developing, please check and implement the same.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #764056
    Hai Prashant,
    The main and only one error "\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Players' has no key defined. Define the key for this EntityType." is due to the primary key is not defined for this model. In EF, the valid table must have a key column to work correctly.
    So define the primary key in the Players table and all should be fine.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764059
    Hi naveen and pawan.

    Thanks for your response. I have given primary key and foreign key in the database tables. But still the same error message.

    Regards

    Prashant Mishra.

  • #764060

    Hi Prashanth.

    It clearly says that you need to specify the key in your entity level. Even if you mention primary key and foreign key in your tables you need to map the tables by writing the table name attributes.
    For example refer below code you can modify according to you.
    You must add the namespace System.ComponentModel.DataAnnotations to make use of key attribute in column level.


    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Web;


    namespace sriEFDemo.Models
    {
    [Table("sri_tblPlayers")]
    public class Players
    {
    [Key]
    public int pid { get; set; } //primary key of table
    public string name { get; set; }
    }
    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.ComponentModel.DataAnnotations;

    namespace sriEFDemo.Models
    {
    [Table("sri_tblTeams")]
    public class Teams
    {
    [Key]
    public int id { get; set; }
    public string name { get; set; }
    }
    }


    Sridhar Thota.
    Editor: DNS Forum.

  • #765640
    Hi Prashant,
    One of the hidden pre-requisite of Entity framework is that each table must have a primary key defined. If you dont define it then a field with name 'Id' or 'PlayerId' will be considered as PrimaryKey. Just specify a primary key column and error will be solved

  • #765645
    Hi Prashant,

    MVC3 will automatically recognize an entity's Key if it follows the convention 'Id' or 'EntityNameId'. Additionally, the entity must expose this as a PROPERTY AND it must be PUBLIC. I made the mistake of using protected for my property and got this error.

    A good example is:

    public int Id { get; set; }
    OR

    public int EntityNameId { get; set; }
    Use the [Key] attribute if you can't follow this convention OR if you want to be very explicit in your code.

    Hope it helps!!

  • #765647
    What concept you are using?
    Creating the DB manually and generating the models automatically based on the db which you created?
    Or
    Creating the models manually and generating the DB automatically?

    You have to specify the primary key and foreign keys. If you did not specify both you will get the exception.

    By Nathan
    Direction is important than speed


Sign In to post your comments