3
votes

I have added a class and created a migration, but when I come to update the db I get an error. Its confusing me because I do have a key! Any ideas?

One or more validation errors were detected during model generation:

ModuleStatus: : EntityType 'ModuleStatus' has no key defined. Define the key for this EntityType. ModuleStatus: EntityType: EntitySet 'ModuleStatus' is based on type 'ModuleStatus' that has no keys defined.

The class

public class ModuleStatus
{
    [Key]
    public int ModuleStatusId { get; set; }

    public Guid ModuleId { get; set; }

    [StringLength(100)]
    public string NetworkAddress { get; set; }

    [StringLength(100)]
    public string ModuleName { get; set; }

    [StringLength(100)]
    public string ModuleDescription { get; set; }

    [StringLength(50)]
    public string ModuleVersion { get; set; }

    public TimeSpan UpTime { get; set; }

    public DateTime LastUpdated { get; set; }
}

The migration looks like this

    public override void Up()
    {
        CreateTable(
            "dbo.ModuleStatus",
            c => new
                {
                    ModuleStatusId = c.Int(nullable: false, identity: true),
                    ModuleId = c.Guid(nullable: false),
                    NetworkAddress = c.String(maxLength: 100),
                    ModuleName = c.String(maxLength: 100),
                    ModuleDescription = c.String(maxLength: 100),
                    ModuleVersion = c.String(maxLength: 50),
                    UpTime = c.Time(nullable: false, precision: 7),
                    LastUpdated = c.DateTime(nullable: false),
                })
            .PrimaryKey(t => t.ModuleStatusId);

    }

Stack trace:

ModuleStatus: : EntityType 'ModuleStatus' has no key defined. Define the key for this EntityType. ModuleStatus: EntityType: EntitySet 'ModuleStatus' is based on type 'ModuleStatus' that has no keys defined.

at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate() at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes() at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext() at ---.---DataContext..ctor() in e:\App Dev\Gazelle - EstateManager\CI-MAIN\---\---\---Context.cs:line 28 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance() at System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType, DbProviderInfo modelProviderInfo, AppConfig config, DbConnectionInfo connectionInfo, Func
1 resolver) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator() at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)

Update 2 Turns out it only fails when I have this line within my DataContext

    public IDbSet<Site> Sites { get; set; } // works fine with this in
    //public IDbSet<ModuleStatus> ModuleStatuses { get; set; } // fails if this is commented in
2
Any chance you are using System.Data.Entity.dll instead of EntityFramework.dll? - Allmighty
Using EntityFramework.dll as far as I know :) - Chris
You don't really need to add the [Key] attribute if the property name is Id or [ClassName]Id, though I would be really surprised if that 'extra' attribute would be the cause... What does your migration look like? - Allmighty
Updated the question with my migration... Our DB has many entities in and I've never seen this problem before. You are right though it looks correct, I cant think what could be causing this - Chris
Maybe one of the atypical fields is causing this. If you remove the GUID and the Timespan fields, do the migration works? - Rafael Merlin

2 Answers

0
votes

May be worth a shot to put this in your DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<ModuleStatus>().HasKey(x = x.ModuleStatusId);

    base.OnModelCreating(modelBuilder);
}

Not sure if this will help, but worth a try.

0
votes

Chris, please refer to the following link: Entity FrameWork Database Migrations

There may be a conflict or ambiguity taking place. The [Key] annotation is somewhat redundant since the overridden Up() method already defines the primary key. Try removing the [Key] annotation and let us know if this solves the issue.