EF模型生成问题

3
我已经添加了一个类并创建了一个迁移,但是当我来更新数据库时出现了错误。这让我感到困惑,因为我确实有一个键!有任何想法吗?
一或多个验证错误在模型生成期间被检测到: 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的机会? - Allmighty
据我所知,使用 EntityFramework.dll :) - Chris
如果属性名是 Id[ClassName]Id,则您实际上不需要添加 [Key] 属性,尽管如果那个“额外”的属性导致问题,我会感到非常惊讶... 您的迁移是什么样子的? - Allmighty
也许是其中一个非典型字段导致了这个问题。如果您移除GUID和Timespan字段,迁移是否可以正常工作? - Rafael Merlin
@JamesShaw 更新了,伙计。 - Chris
显示剩余3条评论
2个回答

0

或许将这段代码放在你的 DbContext 类中会值得一试:

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

    base.OnModelCreating(modelBuilder);
}

不确定这是否有帮助,但值得一试。


0

Chris,请参考以下链接:实体框架数据库迁移

可能存在冲突或歧义。 [Key]注释有些多余,因为覆盖的Up()方法已经定义了主键。尝试删除[Key]注释,然后告诉我们是否解决了问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接