繁体   English   中英

如何使用EF core 2.2在运行时更新模型?

[英]How to Update the models at run-time using EF core 2.2?

我正在使用Entity framework 2.2作为DB first approach进行web API project 我的要求是,当我们向数据库添加新表时,模型应在运行时更新。 我知道如何使用Package Manager Console执行此操作,但是我需要以编程方式进行操作。 我该如何实现?

我试过使用db.Database.Migrate(); startup class

如何确认模型已更新?

您需要编写一个可以使用c#执行powershell命令并在启动函数中调用此方法的方法

public class PowerShellHelper
{
    public void Execute(string command)
    {
        using (var ps = PowerShell.Create())
        {
            var results = ps.AddScript(command).Invoke();
            foreach (var result in results)
            {
                Debug.Write(result.ToString());
            }
        }
    }
}

从数据库更新模型:

Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-Tables>] 
                    [-DataAnnotations] [-Force] [-Project] [-StartupProject] [<CommonParameters>] 

使用--force属性强制更新模型。 我不确定该解决方案是否可以与您一起使用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM