简体   繁体   English

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

[英]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.

相关问题 Microsoft.EntityFrameworkCore.SqlServer - 数据库未在 context.Database.Migrate() 上更新; - Microsoft.EntityFrameworkCore.SqlServer - Database not updating on context.Database.Migrate(); 如何在实体框架中访问context.Database.SqlQuery? - How to access context.Database.SqlQuery in Entity Framework? 如何访问 OnSignedIn .net Core - 实体框架中的数据库数据 - How to access the database data in OnSignedIn .net Core - Entity framework 如何使用 Entity Framework Core 使用泛型枚举迁移类? - How to migrate a class using Generic enums with the Entity Framework Core? 将服务依赖注入到Entity Framework Core数据库上下文中 - Dependency Injection of services into an Entity Framework Core database context 实体框架核心数据库 - Entity Framework Core database 模拟实体框架核心上下文 - Mocking Entity Framework Core context 从Entity Framework模型类访问数据库上下文 - Access database context from Entity Framework model class 如何使用实体框架在上下文之外访问实体的属性? - How to access entity's properties outside context using Entity Framework? 将实体框架生成的数据库迁移到sqlserver - Migrate database generated by entity framework to sqlserver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM