0

I have an error:

One or more validation errors were detected during model generation:

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

in helper class which code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Data.Models;
using Data;

namespace Ambermoda.Web
{
    public class MenuHelper
    {
        public static List<DynamicMenu> GetMenu()
        {
            DataContext db = new DataContext();
            List<DynamicMenu> list = db.ambt_Dynamic_Menu.ToList();

            return list;
        }
    }
}

so I have no idea how can I solve it. I checked everything what (in my opinion) can generate this error, but with any results.

I sam similar posts but any of them doesn't solve my problem.

If anybody have some idea, pleas write it :)

Thanks!

Some more code

DynamicMenu.cs

    [Table("abmt_Dynamic_Menu")]
    public class DynamicMenu
    {
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int dmn_id { get; set; }
        public string dmn_code { get; set; }
        public string dnm_parent_code { get; set; }
        public string dnm_title { get; set; }
        public string dnm_title_en { get; set; }
        public int dnm_order { get; set; }
    }

DataContext.cs

public class DataContext : DbContext
{
    public DataContext() { }

    public DbSet<DynamicMenu> ambt_Dynamic_Menu { get; set; }
}

Connection string:

<add name="DataContext" providerName="System.Data.SqlClient" connectionString="Data Source=Mikasasa-lap\Mikasasa;Database=Ambermoda;Integrated Security=True;Pooling=False;"/>
6
  • 2
    You say you've checked 'everything', did you double (or even triple) check that the DynamicMenu table in the database has a primary key column (and that it's mapped to the entity)? :) That's under the assumption that you're working with database-first. May 22, 2012 at 10:26
  • Show the code for DynamicMenu class, please.
    – Dennis
    May 22, 2012 at 10:30
  • Yes I do. So I add code of classes May 22, 2012 at 10:31
  • 2
    Looking at DynamicMenu.cs we can see your id column has Identity property, but isn't a primary key.
    – lucask
    May 22, 2012 at 10:35
  • @lucask So can you give me some hint how can I add it? May 22, 2012 at 10:36

3 Answers 3

2

According to source code you provided, you're working with the Code First approach.
CF assumes that class should have property named "Id" by the naming convention, and this property will be treated as primary key.

Otherwise, you should manually declare primary key via attribute [Key] or using fluent API (see EntityTypeConfiguration.HasKey method).

1

You need to inform what field is your database Primary Key. Use the attribute Key to do this:

[Table("abmt_Dynamic_Menu")]
public class DynamicMenu
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int dmn_id { get; set; }
    public string dmn_code { get; set; }
    public string dnm_parent_code { get; set; }
    public string dnm_title { get; set; }
    public string dnm_title_en { get; set; }
    public int dnm_order { get; set; }
}
0

Check whether the DyanmicMenu table has primary key

3
  • I have it so you can look at DynamicMenu.cs May 22, 2012 at 10:34
  • @Mateusz Rogulski I cannot access DynamicMenu.cs May 22, 2012 at 10:36
  • ya.. when i browse that url i get this message " Access to this site has been blocked as per Court Orders" May 22, 2012 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.