【发布时间】:2015-08-31 06:54:58
【问题描述】:
所以在我让向导从现有数据库创建模型后,我的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.IPs、context.BannedIPs 和context.Score 上都遇到了错误。我得到的错误是
SnakeGame.Models.ApplicationDbContext 不包含定义 为...
我正试图弄清楚如何解决它。我的 Migrations 文件夹的完整代码可以看到 here。我认为所有这些代码优先迁移的尝试都把我的项目搞得一团糟。废话。
【问题讨论】:
-
你能显示完整的错误吗?
标签: c# asp.net asp.net-mvc entity-framework entity-framework-migrations