首先从 EF 6.1 代码生成 SQL 视图 [英] Generating a SQL view from EF 6.1 code first

查看:16
本文介绍了首先从 EF 6.1 代码生成 SQL 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将我的应用程序从数据库优先更改为代码优先!部署方面有多么大的改进!但现在我有以下问题.我从我的数据库生成了我的代码优先模型,但是在从生成的代码重新创建数据库之后,我的数据库中的视图就像表格一样生成了!

I just changed my application from Database first to code first! What a great improvement in deploying!. But now i have the following problem. I generated my Code-first model from my database, but after recreating the database from the generated code, my views from my database are generated like tables!

如何首先从代码生成视图?如果我需要手动生成它们和/或将它们映射到我的实体?

How do I generate my views from code first? and/or map them to my entities if I need to generate them manually?

编辑.

Luke McGregor 的帖子确实让我很接近.是的,它现在生成视图.但是迁移不起作用.

Luke McGregor's post certainly brought me close. Yes it generates the views now. But the migrations don't work.

当尝试执行 Update-Database 语句时,初始输出是仍有代码更改.

When trying to do a Update-Database statement the initial output is that there still are code changes.

因此我执行了 Add-Migration xxx 命令并再次触发了 Update-Database 命令.

I therefore executed the Add-Migration xxx Command and fired the Update-Database command again.

解决了我的代码优先代码和视图的 SQL 代码之间的一些差异解决了这个问题!

Resolving a few differences between my Code-first code and the view's SQL code solved this issue!

推荐答案

您需要创建一个手动迁移,其中包含一些原始 SQL,例如以下内容

You will need to create a manual migration with some raw SQL in it eg something along the lines of the below

public partial class MyMigration: DbMigration 
{ 
    public override void Up() 
    { 
        Sql("CREATE VIEW......"); 
    } 
}

这篇关于首先从 EF 6.1 代码生成 SQL 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆