在实体框架中使用代码优先更新数据库时出错

时间:2013-09-15 08:50:50

标签: asp.net-mvc asp.net-mvc-3 entity-framework asp.net-mvc-4 ef-code-first

我是MVC4.0的新手。我创建了一个示例Internet应用程序。我添加了一个类

public class User
{
    public int UserId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
 }

现在,我想在用户表中添加一个名为MiddleName的新列。

我已经添加了以下属性。

    public string LastName { get; set; }

我在项目中启用了迁移。

现在,我想用这个新列(MiddleName)更新数据库。

我在Package Manager Console中尝试了以下语法

PM> Update-Database

这给了我以下错误......

Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
System.Data.Entity.Core.MetadataException: Schema specified is not valid. Errors: 
(0,0) : error 0005: The 'Namespace' attribute is invalid - The value 'MvcApplication14._5' is invalid according to its datatype 'http://schemas.microsoft.com/ado/2009/11/edm:TNamespaceName' - The Pattern constraint failed.
   at System.Data.Entity.Core.Metadata.Edm.EdmItemCollection.LoadItems(IEnumerable`1 xmlReaders, IEnumerable`1 sourceFilePaths, SchemaDataModelOption dataModelOption, DbProviderManifest providerManifest, ItemCollection itemCollection, Boolean throwOnError)
   at System.Data.Entity.Core.Metadata.Edm.EdmItemCollection.Init(IEnumerable`1 xmlReaders, IEnumerable`1 filePaths, Boolean throwOnError)
   at System.Data.Entity.Core.Metadata.Edm.EdmItemCollection..ctor(IEnumerable`1 xmlReaders)
   at System.Data.Entity.Utilities.XDocumentExtensions.GetStorageMappingItemCollection(XDocument model, DbProviderInfo& providerInfo)
   at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel, XDocument targetModel, Lazy`1 modificationCommandTreeGenerator, MigrationSqlGenerator migrationSqlGenerator)
   at System.Data.Entity.Migrations.DbMigrator.IsModelOutOfDate(XDocument model, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate 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()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Schema specified is not valid. Errors: 
(0,0) : error 0005: The 'Namespace' attribute is invalid - The value 'MvcApplication14._5' is invalid according to its datatype 'http://schemas.microsoft.com/ado/2009/11/edm:TNamespaceName' - The Pattern constraint failed.

2 个答案:

答案 0 :(得分:0)

首先,您应该在程序包管理器控制台中使用此命令启用迁移:

Enable-Migrations

此命令将Migrations文件夹添加到项目中, 之后,您可以更改模型,例如将此属性添加到模型中:

public string LastName { get; set; }

然后您可以使用此命令进行更新:

Update-Database -Verbose
  

我们将指定 -Verbose 标志,以便我们可以看到正在运行的SQL   反对数据库。

答案 1 :(得分:0)

它不喜欢命名空间MvcApplication14._5中的点下划线。删除点或下划线,它应该工作。