EF model creation problem

I added a class and created a port, but when I come to update the db I get an error. It confuses me because I have the key! Any ideas?

One or more validation errors were encountered during model generation:

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

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

    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. define a key for this EntityType. ModuleStatus: EntityType: EntitySet "ModuleStatus" is based on the "ModuleStatus" type, which 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.DbModelBuild (DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuild System.Data.Entity.Internal.LazyInternalContext.CreateModel (LazyInternalContext internalContext) when 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) in System.Data.Entity.Migrations.DbMigrator..ctor (DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) when System.Data.Entity.Migrations.DbMigrator..ctor (DbMigrationsConfiguration configuration) .Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator () in System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run () in System.AppDomain.DoCallBack (CrossAppDomainDelegate callBackDelegomain.App) 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 () in System.Data.Entity.Migrations.MigrationsDomainCommand.Execute (Action command)

Update 2 It turns out it fails when I have this line in myDataContext

    public IDbSet<Site> Sites { get; set; } // works fine with this in
    //public IDbSet<ModuleStatus> ModuleStatuses { get; set; } // fails if this is commented in

      

+3


source to share


2 answers


It might be worth putting 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


source


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



There may be conflict or ambiguity. The [Key] annotations are somewhat redundant because the overridden Up () method already defines the primary key. Try removing the [Key] annotation and let us know if that solves the problem.

0


source







All Articles