错误播种数据库? (MVC 4申请,EF 5,代码优先)

时间:2014-03-13 16:37:05

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

我已在我的项目中成功添加了新迁移,但当我在程序包管理器控制台中运行update-database时,我收到:Error Seeding Privileges: An error occurred while updating the entries. See the inner exception for details.

完整详情:

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.
System.Exception: Error Seeding Privileges: An error occurred while updating the entries. See the inner exception for details.
   at PersonalPortfolio.Migrations.Configuration.Seed(PortfolioContext context) in c:\James-Projects\TRAINING\PersonalPortfolio\PersonalPortfolio\Migrations\Configuration.cs:line 56
   at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context)
   at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
   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)

以下是我的权限种子方法:

namespace PersonalPortfolio.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using PersonalPortfolio.Models;

    internal sealed class Configuration : DbMigrationsConfiguration<PersonalPortfolio.DAL.PortfolioContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(PersonalPortfolio.DAL.PortfolioContext context)
        {
            // The different Privileges a Visitor can have
            #region Privileges
            try
            {
                var privileges = new List<Privileges>
                {
                    new Privileges { Privilege = "Root" },              // ME
                    new Privileges { Privilege = "Admin" },             // Admin
                    new Privileges { Privilege = "Outsider" },          // Random Net Visitor
                    new Privileges { Privilege = "Client" },            // Tommy Peterson, Joe Dorris, Jim Dorris
                    new Privileges { Privilege = "Client Primary" }     // Joe Dorris, Jim Dorris
                };

                foreach (Privileges priv in privileges)
                {
                    var recordInDb = context.Privileges.Where(p => p.Privilege == priv.Privilege).FirstOrDefault();
                    if (recordInDb == null)
                    {
                        context.Privileges.Add(priv);
                    }
                }
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception("Error Seeding Privileges: " + ex.InnerException.Message);
            }
            #endregion
        }
    }
}

任何人都有如何解决此问题的想法?

1 个答案:

答案 0 :(得分:0)

您的特权课程是什么样的?我猜你的主键不是'Id',这是EF期望的,以便自动将其创建为标识列。如果是这种情况,那么您需要将其重命名为“Id”或使用[Key]注释指定您自己的主键名称。

public class Privileges
    {
        public int Id { get; set; }
        ...

public class Privileges
    {
        [Key]
        public int MyId { get; set; }
         ...