错误播种数据库? (MVC 4应用,EF 5,code-前) [英] Error Seeding Database? (MVC 4 Application, EF 5, Code-First)

查看:431
本文介绍了错误播种数据库? (MVC 4应用,EF 5,code-前)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功地在我的项目增加了一个新的迁移,但是当我运行更新数据库软件包管理器控制台我收到:错误播种权限:更新条目时发生错误。详情请参阅内部异常。

I've successfully added a new migration in my project, but when I run update-database in Package Manager Console I receive: 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)

下面是我的特权种子的方法:

Below is my Privileges seed method:

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
        }
    }
}

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

Anyone have ideas for how to fix this?

推荐答案

什么是您的特权阶级的样子?我猜你的主键是不是叫'ID',这是EF预计,以自动创建它作为一个标识列。如果是这种情况,那么你就需要将其重命名为ID或使用[关键]批注指定自己的主键的名称。

What does your Privileges class look like? I'm guessing that your primary key isn't called 'Id', which is what EF expects in order to automatically create it as an identity column. If this is the case then you'll need to rename it to 'Id' or use the [Key] annotation to specify your own primary key name.

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

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

这篇关于错误播种数据库? (MVC 4应用,EF 5,code-前)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆