[英]Entity Framework Core - How To Access Context.Database.Migrate()
I am just getting started with EF Core and Net Core in general an ran into a problem I could not find any answers for. 我只是刚开始使用EF Core和Net Core,遇到了一个我找不到任何答案的问题。
I am working on a console application that uses a SQLite database for storage. 我正在使用SQLite数据库进行存储的控制台应用程序。 I am testing things right now and working with the context works fine. 我现在正在测试事物,并且使用上下文可以正常工作。 My example program below runs fine. 我下面的示例程序运行良好。 Note that I did use migrations to create the database initially. 请注意,我确实使用迁移来最初创建数据库。
Now eventually when I finish this App I want to make sure that the database exists. 现在最终,当我完成此应用程序时,我想确保数据库存在。 As read in other posts this should be done with ctx.Database.Migrate()
. 如其他文章中所述,应使用ctx.Database.Migrate()
完成此操作。 I cannot access this method yet however. 但是,我无法使用此方法。 So my question is what do I have to do to access it? 所以我的问题是我必须做什么才能访问它? Am I missing a package that adds an extension method? 我是否缺少添加扩展方法的软件包? Do I need to configure more things? 我需要配置更多东西吗?
Please excuse this very basic question but I could not find anything regarding this. 请原谅这个非常基本的问题,但我对此一无所获。 So if I just don't know where to look I would also be glad about a reading recommendation. 因此,如果我不知道在哪里看,我也会为阅读建议感到高兴。
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
}
}
Many EF Core methods are implemented as extension methods. 许多EF Core方法被实现为扩展方法。 So to make them available, this first thing you need is: 因此,要使它们可用,您需要做的第一件事是:
using Microsoft.EntityFrameworkCore;
This particular method is defined in RelationalDatabaseFacadeExtensions
residing in the Microsoft.EntityFrameworkCore.Relational
assembly, so make sure you are referencing it. 此特定方法在Microsoft.EntityFrameworkCore.Relational
程序集中的RelationalDatabaseFacadeExtensions
中定义,因此请确保引用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.