c# - Entity Framework Core - 如何访问 Context.Database.Migrate()

标签 c# sqlite .net-core entity-framework-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
    }
}

最佳答案

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

using Microsoft.EntityFrameworkCore;

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

关于c# - Entity Framework Core - 如何访问 Context.Database.Migrate(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47408392/

相关文章:

c# - C# 中从字符串中删除换行符的最简单方法是什么?

c# - 为什么不鼓励使用 Debug.Print?

c# - 无法确定类型为 “System.Data.Sqlite.SqliteFactory” 的提供程序工厂的提供程序名称

linq - 如何使用 LINQ 连接子查询

entity-framework - Entity Framework Core 2.1 在左连接上抛出 ArgumentException

c# - 自定义验证属性调用另一个验证属性

c# - 汇编成字节

sql - 如何在sqlite中预先存在的数据表的开头插入X行

android - 如何在服务中使用 SQLiteOpenHelper

c# - 防止 Simple.OData.Client 获取整个结构