Chris
Chris

Reputation: 27384

EF Model Generation issue

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

Upvotes: 3

Views: 861

Answers (2)

James Shaw
James Shaw

Reputation: 839

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.

Upvotes: 0

danludwig
danludwig

Reputation: 47375

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.

Upvotes: 0

Related Questions