c# - 通用 Windows 平台的 EF 迁移

标签 c# entity-framework uwp

我有以下 DbContext,我想使用 Entity Framwork 迁移它

public class TestDbContext: DbContext
{
    public DbSet<State> States { get; set; }
    public DbSet<StateType> StateTypes { get; set; }
    public DbSet<Measure> Measures { get; set; }
    public DbSet<Priority> Priorities { get; set; }
    public DbSet<Task> Tasks { get; set; }
    public DbSet<TaskType> TaskTypes { get; set; }
    public DbSet<Document> Documents { get; set; }


    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        string databaseFilePath = "test.db";
        try
        {
            databaseFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, databaseFilePath);
        }
        catch (InvalidOperationException) { }
        optionsBuilder.UseSqlite($"Data source={databaseFilePath}");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

    }

我运行了语句

enable-migrations -ContextTypeName TestData.TestDbContext

在程序包管理器控制台中生成配置。 但是生成有编译错误,因为找不到以下命名空间/类:

using System.Data.Entity;
using System.Data.Entity.Migrations;
DbMigrationsConfiguration

.

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

    internal sealed class Configuration : DbMigrationsConfiguration<TestDatas.TestDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(TestDatas.TestDbContext 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" }
            //    );
            //
        }
    }
}

关于如何编译配置以便我可以添加迁移的任何解决方案?

最佳答案

documentation on EF7可能会有帮助。

我测试过,DbContext 应该使用这个引用:

using Microsoft.Data.Entity; 

using System.Data.Entity.Migrations;也应该改为

using Microsoft.Data.Entity.Migrations;

关于c# - 通用 Windows 平台的 EF 迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33994214/

相关文章:

c# - 在 C# 中创建结构的浅拷贝

c# - 有没有办法避免注释属性中的魔数(Magic Number)?

c# - LINQ EF 未保存到数据库

c# - 检测 Windows 10 系统电源状态

c# - Frame.GoBack(NavigationTransitionInfo) 回调

c# - 搜索树/特里时返回多个节点?

c# - 使用字典中的抽象泛型类作为值而不具体说明类型

c# - 一对零或一的关系 : Cannot insert explicit value for identity column in table when IDENTITY_INSERT is OFF

c# - 将 Include() 调用替换为 Select()

c# - UWP CreateFileAsync 线程在 Release模式下编译并选中 Compile with .NET Native 工具链时退出