1

我是 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) 更新数据库。

我在包管理器控制台中尝试了以下语法

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.
4

2 回答 2

0

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

于 2014-01-21T14:27:22.143 回答
0

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

Enable-Migrations

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

public string LastName { get; set; }

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

Update-Database -Verbose

我们将指定–Verbose标志,以便我们可以看到针对数据库运行的 SQL。

于 2013-09-15T16:59:03.620 回答