Error Seeding Database? (MVC 4 Application, EF 5, Code-First)

孤人 提交于 2020-01-06 08:27:12

问题


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

Full 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?


回答1:


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

or

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


来源:https://stackoverflow.com/questions/22385151/error-seeding-database-mvc-4-application-ef-5-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!