我添加了一个类并创建了一个迁移,但是当我来更新db时,我收到一个错误。令我困惑的是因为我有一把钥匙!有什么想法吗?
在模型生成期间检测到一个或多个验证错误:
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'没有钥匙 定义
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) System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)at System.Data.Entity.Internal.RetryLazy
2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
1个解析器)at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置,DbContext usersContext,DatabaseExistenceState presenceState,布尔名为ByBreateDatabase)at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置) System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator() 在 System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() 在System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
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
在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner) 跑者) System.Data.Entity.Migrations.Design.ToolingFacade.Update(字符串 targetMigration,Boolean force)at System.Data.Entity.Migrations.UpdateDatabaseCommand<> c__DisplayClass2< .ctor> b__0() 在 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(动作 命令)
更新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
答案 0 :(得分:0)
将这个放在你的DbContext类中可能值得一试:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ModuleStatus>().HasKey(x = x.ModuleStatusId);
base.OnModelCreating(modelBuilder);
}
不确定这是否有帮助,但值得一试。
答案 1 :(得分:0)
Chris,请参阅以下链接:Entity FrameWork Database Migrations
可能存在冲突或歧义。 [Key]注释有点多余,因为重写的Up()方法已经定义了主键。尝试删除[Key]注释,如果这样可以解决问题,请告诉我们。