我在使用DbMigrator执行迁移时遇到问题,不管怎样,迁移类都没有执行,当然也没有反映在数据库中。
我正在尝试实现的是将迁移分组到版本。基本上我有一个Version基类,其中所有版本都派生自。
例如,我有一个版本1000,对应于版本1.0.0.0。
版本类有一个名为ExecuteMigrations的方法,它负责执行与其对应的版本相关的迁移。
以下是版本类的示例(Version1000 for 1.0.0.0),请参阅使用DbMigrator的ExecuteMigrations方法,它还使用从DbMigrationsConfiguration派生的VersionConfiguration类,它在那里我设置名称空间,目录, ContextType ..等..
public class Version1000 : Version
{
public override string VersionNumber
{
get { return "1.0.0.0"; }
}
public static Version Version
{
get
{
return new Version1000();
}
}
public override void ExecuteMigrations()
{
var configuration = new VersionConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(MyCustomContext.CONTEXT_CONNECTION_STRING_NAME);
var migrator = new DbMigrator(configuration);
migrator.Update();
}
}
VersionConfiguration类
class VersionConfiguration : DbMigrationsConfiguration<MyCustomContext>
{
public VersionConfiguration()
{
AutomaticMigrationsEnabled = false;
MigrationsNamespace = "Some.Namespace._1000";
MigrationsDirectory = "Migration\\1000";
ContextType = typeof(MyCustomContext);
}
protected override void Seed(MyCustomContext context)
{
base.Seed(context);
}
}
在我的应用程序的应用程序启动时,我所做的是检查我的数据库的当前版本,然后应用未应用于当前数据库的版本。这是通过调用版本类的ExecuteMigrations方法来完成的。
问题是我没有得到任何例外,只是在调用DbMigrator.Update方法时没有反映到数据库中,并且在DbMigrationsConfiguration中设置的文件夹/命名空间中的迁移类从未执行过
应用程序类型:ASP.NET MVC 4 .NET Framework:4.5
答案 0 :(得分:1)
您的迁移还应实现IVersionMetadata,并且必须提供Id,Source和Target属性。它们的值应与以下值相似:
Id = string.Format("{0}_{1}", SomeSequenceNumber.ToString().PadLeft(15, '0'), "Name")
Source = null
Target = "H4sIAAAAAAAEAL1Y23LbNhB970z/gYOn9iGCJEdJ66GSsWW78bSyO6aT1wxErmRMcGEJ0BX7a33oJ/UXuryKF11o2e6bCKx2z1lwD47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MeRSLiC4VrexDWN7ZgzIhUjqVF26v37mG5uY3dswm8uXoIAwOVKAW3UecxFUuK/yU6I9UqR6/AvgejZK6Lox3SqpMt1o1TNR0b4LCEEFeN/cgwwFJjO3ymOPsBvb4R42O+ZecIaTL02RY/P99I8Vmv6z8uE/KRLfnIsRAAA=";
可能对Target参数有一些澄清:它包含整个模型的压缩版本。他们想要消除在EF中提供Target的必要性,但是,即使EF6 alpha3仍然需要这样。上面给出的字符串是空模型的压缩格式,我只是把它作为一个常量,它就像一个魅力......
另外:在我的项目中,我使用了错误的Id格式,但它没有做任何事情。需要尽快在EF资源中找到更多相关内容,但是,现在也许这有助于您的情况!
答案 1 :(得分:-1)
正如代码优先所说,你必须描述你的实体:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace StartReg.Data {
public class UserSession {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserSessionId { get; set; }
[Required]
[MaxLength(100)]
public string UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
[Required]
public DateTimeOffset LogonTime { get; set; }
[Required]
[MaxLength(40)]
public string LogonIPAddress { get; set; }
[Required]
[MaxLength()]
public string LogonUserAgent { get; set; }
}
}
将其添加到DbContext:
namespace StartReg.Data {
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
public partial class StartRegDb : DbContext {
public StartRegDb()
: base("DefaultConnection") {
}
public DbSet<Distance> Distances { get; set; }
}
}
然后当您在Package Manager Console中执行(是否安装了NuGet?)命令Enable-Migrations
和Add-Miration Version1000
时,您将收到迁移配置
namespace StartReg.Migrations {
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
using StartReg.Data;
internal sealed class Configuration : DbMigrationsConfiguration<StartReg.Data.StartRegDb> {
public Configuration() {
AutomaticMigrationsEnabled = false;
}
protected override void Seed(StartReg.Data.StartRegDb context) {
// This method will be called after migrating to the latest version.
}
}
}
和Version1000迁移类:
命名空间StartReg.Migrations { 使用系统; 使用System.Data.Entity.Migrations;
public partial class Initial : DbMigration {
public override void Up() {
CreateTable(
"dbo.Distances",
c => new {
DistanceId = c.Guid(nullable: false),
MeetingId = c.Int(nullable: false),
Key = c.String(nullable: false, maxLength: 20),
Name = c.String(nullable: false, maxLength: 100),
Ordering = c.Short(nullable: false),
AllowMale = c.Boolean(nullable: false),
AllowFemale = c.Boolean(nullable: false),
})
.PrimaryKey(t => t.DistanceId)
.ForeignKey("dbo.Meetings", t => t.MeetingId, cascadeDelete: true)
.Index(t => t.MeetingId)
.Index(t => new { MeetingId = t.MeetingId, Ordering = t.Ordering });
}
public override void Down() {
DropTable("dbo.Distances");
}
}
}
之后你需要在任何实际的数据库工作之前放置一些地方(例如Application_Start):
Database.SetInitializer(new MigrateDatabaseToLatestVersion<StartRegDb, StartReg.Migrations.Configuration>());
当应用程序启动时 - 表格距离会自动在数据库中创建。