一或多个验证错误在模型生成期间被检测到: ModuleStatus: : EntityType 'ModuleStatus' 未定义键。请为此EntityType定义键。ModuleStatus: EntityType: EntitySet 'ModuleStatus' 基于类型'ModuleStatus',该类型未定义键。
这个类
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; }
}
迁移的过程如下所示。
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);
}
堆栈跟踪:
ModuleStatus: : EntityType 'ModuleStatus'未定义键。请为此EntityType定义键。 ModuleStatus:EntityType:EntitySet“ModuleStatus”基于未定义键的类型“ModuleStatus”。
在System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()中 在System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)中 在System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)中 在System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)中 在System.Data.Entity.Internal.RetryLazy<code> 2.GetValue(TInput input)中 在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()<br/ >在System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes()中 在System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()中 在---.---DataContext..ctor()中 E:\ App Dev \ Gazelle-EstateManager \ CI-MAIN \ --- \ --- \ --- Context.cs中的第28行 ---异常抛出的前一个位置的堆栈跟踪结束--- 在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()中 在System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()中 在System.Data.Entity.Infrastructure.DbContextInfo..ctor(Type contextType,DbProviderInfo modelProviderInfo,AppConfig config,DbConnectionInfo connectionInfo,Func 1解析器)中 在System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration配置,DbContext usersContext,DatabaseExistenceState existenceState,Boolean calledByCreateDatabase)中 在System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration配置)中 at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()中的System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()中 在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)中 在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)中<br/ >在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)中 在System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration,Boolean force)中 在System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()中 在System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
更新 2
原来只有当我在DataContext
中有这一行时才会失败。
public IDbSet<Site> Sites { get; set; } // works fine with this in
//public IDbSet<ModuleStatus> ModuleStatuses { get; set; } // fails if this is commented in
System.Data.Entity.dll
而不是EntityFramework.dll
的机会? - AllmightyEntityFramework.dll
:) - ChrisId
或[ClassName]Id
,则您实际上不需要添加[Key]
属性,尽管如果那个“额外”的属性导致问题,我会感到非常惊讶... 您的迁移是什么样子的? - Allmighty