按照实体代码优先方法播种我的数据库

Seeding my database as per the Entity code-first approach

所以在我让向导从现有数据库创建模型后,我的 Configuration.cs

namespace SnakeGame.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<SnakeGame.Models.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(SnakeGame.Models.ApplicationDbContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}

我的数据库模型是

namespace SnakeGame.Migrations
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;

    public partial class SnakeDB : DbContext
    {
        public SnakeDB()
            : base("name=SnakeDB")
        {
        }

        public virtual DbSet<BannedIP> BannedIPs { get; set; }
        public virtual DbSet<GameLog> GameLogs { get; set; }
        public virtual DbSet<IP> IPs { get; set; }
        public virtual DbSet<Score> Scores { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<GameLog>()
                .Property(e => e.logText)
                .IsUnicode(false);

            modelBuilder.Entity<IP>()
                .HasMany(e => e.BannedIPs)
                .WithRequired(e => e.IP)
                .WillCascadeOnDelete(false);

            modelBuilder.Entity<Score>()
                .Property(e => e.name)
                .IsUnicode(false);
        }
    }
}

为了按照注释掉的说明进行操作,我将 protected override void Seed(SnakeGame.Models.ApplicationDbContext context) 的正文更改为

        context.IPs.AddOrUpdate(
            i => i.id,
            new IP { id = 1, byte1 = 4, byte2 = 35, byte3 = 241, byte4 = 179 },
            new IP { id = 2, byte1 = 172, byte2 = 16, byte3 = 254, byte4 = 1 }
        );

        context.BannedIPs.AddOrUpdate(
            i => i.id,
            new BannedIP { id = 1, ipId = 1}
        );

        context.Score.AddOrUpdate(
            s => s.id,
            new Score {  id = 1, score1 = 12, name = "John Skeet" },
            new Score {  id = 2, score1 = 1923, name = "Steve Ballmer"}
        );

但是我在 context.IPscontext.BannedIPcontext.Score 上都遇到了错误。我得到的错误是

SnakeGame.Models.ApplicationDbContext does not contain a definition for ...

我正在想办法解决这个问题。我的Migrations文件夹的完整代码可以看here。我想我已经通过所有这些代码优先迁移的尝试把我的项目搞得一团糟。废话。

您是否使用迁移更新了数据库??我的项目中有一个命令示例

/*
* X DATA CONTEXT 
*/

//this command enables migrations
Enable-Migrations -StartUpProjectName X.Web -ProjectName X.DAL.Repository -EnableAutomaticMigrations -ContextTypeName XDataContext -Verbose -MigrationsDirectory:XDataContextMigrations

//this command create a new migration, please set up the name of the DBMigration
Add-Migration [NAME] -StartUpProjectName X.Web -ProjectName X.DAL.Repository -ConfigurationTypeName XUpdateDatabaseInitializer -Verbose 

//this command updates the current database
Update-Database -StartUpProjectName X.Web -ProjectName X.DAL.Repository -ConfigurationTypeName XUpdateDatabaseInitializer -Verbose 

//this command rollbacks the current Database to Specific Target applying Down() methods
Update-Database –TargetMigration: SpecificTargetMigrationName -StartUpProjectName X.Web -ProjectName X.DAL.Repository -ConfigurationTypeName XUpdateDatabaseInitializer -Verbose 

因此,如果您已经更新了数据库,我建议您完全删除当前数据库并使用迁移命令重新创建它...

希望对您有所帮助。

你的实体都是在 SnakeDb 上下文中定义的,而不是 ApplicationDbContext 所以将你的种子更改为

protected override void Seed(SnakeGame.Migrations.SnakeDb context)
...

进行逆向工程后,您可能希望将内容移动到您希望应用具有的任何结构中。对我来说,我将 POCO 复制到一个单独的 "Entity" 项目,然后将 EF 和上下文内容移动到一个 "Data" 项目,但您也可以将它们移动到不同的文件夹中。

其次,由于您对数据库进行了逆向工程,因此您将需要基线初始迁移。您可以注释掉 Up() 和 Down() 代码,也可以通过

生成
Add-Migration InitialCreate –IgnoreChanges

https://msdn.microsoft.com/en-us/data/dn579398.aspx#option1