【问题标题】:EF Model Generation issueEF 模型生成问题
【发布时间】:2015-03-06 13:44:03
【问题描述】:

我添加了一个类并创建了一个迁移,但是当我来更新数据库时出现错误。这让我很困惑,因为我确实有一把钥匙!有什么想法吗?

在模型生成过程中检测到一个或多个验证错误:

ModuleStatus: : EntityType 'ModuleStatus' 没有 键定义。定义此 EntityType 的键。模块状态: 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”基于没有键的“ModuleStatus”类型 已定义。

在 System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate() 在 System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) 在 System.Data.Entity.DbModelBuilder.Build(DbConnection 提供者连接)在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext 内部上下文)在 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 解析器)在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration 配置、DbContext usersContext、DatabaseExistenceState 存在状态,布尔调用由创建数据库)在 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) 在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner 亚军)在 System.Data.Entity.Migrations.Design.ToolingFacade.Update(字符串 targetMigration,布尔力)在 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

【问题讨论】:

  • 您是否有机会使用System.Data.Entity.dll 而不是EntityFramework.dll
  • 据我所知使用EntityFramework.dll :)
  • 如果属性名称是Id[ClassName]Id,您实际上不需要添加[Key] 属性,但如果那个“额外”属性是原因,我会感到非常惊讶...您的迁移情况如何?
  • 用我的迁移更新了问题...我们的数据库中有很多实体,我以前从未见过这个问题。你是对的,虽然它看起来是正确的,但我想不出是什么原因造成的
  • 可能是非典型字段之一导致了这种情况。如果您删除 GUID 和 Timespan 字段,迁移是否有效?

标签: c# entity-framework


【解决方案1】:

可能值得尝试将其放入您的 DbContext 类中:

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

    base.OnModelCreating(modelBuilder);
}

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

【讨论】:

    【解决方案2】:

    克里斯,请参考以下链接:Entity FrameWork Database Migrations

    可能会发生冲突或歧义。 [Key] 注释有些多余,因为重写的 Up() 方法已经定义了主键。尝试删除 [Key] 注释,如果这能解决问题,请告诉我们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-25
      • 2011-09-29
      • 2013-12-29
      相关资源
      最近更新 更多