实体框架核心 - 如何访问Context.Database.Migrate()

时间:2017-11-21 08:25:03

标签: c# sqlite .net-core ef-migrations ef-core-2.0

我刚刚开始使用EF Core和Net Core,遇到了一个我无法找到答案的问题。

我正在使用一个使用SQLite数据库进行存储的控制台应用程序。我现在正在测试一些东西并且使用上下文工作正常。我的示例程序运行正常。请注意,我最初使用迁移来创建数据库。

现在最终当我完成这个应用程序时,我想确保数据库存在。正如其他帖子中所述,这应该使用ctx.Database.Migrate()来完成。但是,我无法访问此方法。所以我的问题是我需要做什么才能访问它?我错过了一个添加扩展方法的包吗?我需要配置更多东西吗?

请原谅这个非常基本的问题,但我找不到任何有关此问题的内容。因此,如果我不知道在哪里看,我也会对阅读建议感到高兴。

using System;
using MyLog.NetCore.Models;
using MyLog.NetCore.DataAccess;

namespace MyLog.NetCore
{
    internal class Program
    {
        #region Private Methods

        private static void Main(string[] args)
        {
            using (var ctx = new MyLogContext())
            {
                ctx.Add(new PartialLogEntry { PartialLogEntryID = 1, StartDateTime = 1, Title = "Test" });
                var count = ctx.SaveChanges();
                Console.WriteLine($"{count} changes saved to database!");

                Console.WriteLine();
                Console.WriteLine("All partial lof entries in database:");
                foreach (var entry in ctx.PartialLogEntries)
                {
                    Console.WriteLine($"ID: {entry.PartialLogEntryID}\tStart: {entry.StartDateTime}\tTitle: {entry.Title}");
                }
            }

            Console.ReadLine();
        }

        #endregion Private Methods
    }
}

1 个答案:

答案 0 :(得分:6)

许多EF Core方法都是作为扩展方法实现的。所以,要使它们可用,首先需要的是:

using Microsoft.EntityFrameworkCore;

此特定方法在驻留在RelationalDatabaseFacadeExtensions程序集中的Microsoft.EntityFrameworkCore.Relational中定义,因此请确保您引用它。