gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:16:56 29 4
gpt4 key购买 nike

我刚刚开始使用 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com