0

I'm currently in possession of one VS solution made by someone else that use Entity Framework. This solution include one console application to test the main features that runs just fine.

However, if I try to call these main feature in my own VS solution (with the dll references), I have the following error message:

ConsoleApplication1.Persistence.FileTemplateModel: EntityType 'FileTemplateModel' has no key defined. Define the key for this EntityType.

I searched through Internet and I found this one that seems very similar: EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType

However, it seems that it’s something else since the console application defines one TestContext that inherits the FrameworkDbContext that inherits DbContext.

public partial class FrameworkDbContext : DbContext
{
        public FrameworkDbContext(): base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {


public class TestContext : FrameworkDbContext
{
        public override UserModel CurrentUser
        {
            get
            {
                if (_currentUser == null)
                {

EDIT: The FileTemplateModel from the VS solution (made by someone else) already declare which property is the primary key:

[Table("File.Template", Schema = "Template")]
public class FileTemplateModel : _Auditable
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    [HiddenInput(DisplayValue = false)]
    public int FileTemplateId { get; set; }

    [Required]
    [Display(Name = "Type")]
    public int FileTemplateTypeId { get; set; }
    [ForeignKey("FileTemplateTypeId")]
    public virtual FileTemplateTypeModel FileTemplateType { get; set; }

    [Required]
    [Display(Name = "Name")]
    public string Name { get; set; }

In fact, as I already mentioned, everything works just fine if I run the console application test included it that VS solution, meaning that FileTemplateModel is correctly declared. The problem is when I try to build my own console application in one VS solution completely separated.

GOT IT: Well, it seems that the model’s annotations are lost somehow. So I decided to force them with code first at OnModelCreating:

modelBuilder.Entity<FileTemplateModel>().HasKey(x => x.FileTemplateId);

Everything works just fine now, but I wonder: why those annotations at FileTemplateModel model are not enough to define the primary key?

Community
  • 1
  • 1
  • 2
    The error message means that EF needs some primary key, so that it can uniquely identify a row (entity) in a database table which does not seems to be specified on `FileTemplateModel`. Can you post the definition of your type `FileTemplateModel`? Are you using code first or database first in the EF code? – Martin Nov 06 '15 at 12:53
  • There are three things to consider here. A context and an object that is being evaluated in that context mostly part of an IDSet<(objectName)>. If you are being errored to 'FileTemplateModel' not having a key. All the updating of the context in the world will not help it not having a key error. Three what is the config of the project that works showing the connection as? Generally Entity Framework inserts a whole config section of how to connect to it's object and you can reverse engineer that for a console app and install Entity from NuGet to your new project as well. – djangojazz Nov 06 '15 at 16:12

0 Answers0