如何在 EF 6.1 CodeFirst 中的视图上添加导航属性

本文介绍了如何在 EF 6.1 CodeFirst 中的视图上添加导航属性的处理方法,对大家解决问题具有一定的参考价值

问题描述

让我们举个例子来解释我的问题.

Let's make a case to explain my problem.

MyTable1

+id

+myTable2Id

+myTable2Id

MyTable2

+id

我的视图1

+id

+myTable2Id

+myTable2Id

MyView1 存在于案例中,来自MyTable1 的数据.现在我想从我的视图中的 EF6.1 代码优先方法创建一个导航属性到 MyTable2.

MyView1 exists in the case, from data from the MyTable1. Now i want to create a Navigation property from my EF6.1 Code first approach in my View to MyTable2.

我知道数据库优先的方法是可能的,但代码优先的方法也有可能吗?

I know that it was possible from the database first approach, but is it also possible from the code-first approach and how?

我在网上搜索了一些,但由于 View 这个词的含义很多,很难找到关于它的信息.

I search some on internet, but due many meanings of the word View, it's very hard to find information on it.

同样使用我尝试过的代码中的方法,我总是收到无法完成迁移的错误.因为迁移尝试向视图添加外键,这是不可能的.

Also with the approaches in codes that i tried, i always get an error that the migration can't be completed. Because the Migration tries to add an foreign key to the view, which isn't possible.

详细说明我的解释.我希望能够通过以下方式在代码中处理它:

To elaborate a bit more on my explanation. I want to be able to approach it in code the following way:

Guid table2Id = context.MyView1.FirstOrDefault().MyTable2.id;

我会详细说明一下,看看我能否更好地解释我的问题.

I will eleborate a bit more, to see if i can get my problem better explained.

当我将以下内容添加到我的视图实体中时:

When i added the following to my view Entity:

public virtual MyTable2 Table2 { get; set;}

EF 会自动生成如下迁移:

EF will automaticly generate the following migration:

public override void Up() {
    CreateIndex("MyView1", "MyTable2Id");
    AddForeignKey("MyView1", "MyTable2Id", "MyTable2", "id")
}

在运行 update-database 时会出现以下错误:

Which on running update-database gives the following error :

无法在视图 'MyView1' 上创建索引,因为该视图未绑定架构"

"Cannot create index on view 'MyView1' because the view is not schema bound"

在迁移不是石头的评论的帮助下......并且是可变的,我做到了.

With help of the comment that the migration aren't of stone.. and are changeable i made it.

我使用了以下 fluentAPI:

I used the following fluentAPI:

    // Map one-to-zero or one relationship 
    modelBuilder.Entity<MyTable2>()
        .HasRequired(t => t.MyTable1)
        .WithOptional(t => t.MyTable2);

    modelBuilder.Entity<MyTable1>()
        .HasOptional(t => t.MyTable2);

并将我的表格更改为:(MyTable2 的 FK 并从视图中删除)

And changing my tables to this: (The FK to the MyTable2 and removed from the view)

MyTable1

+id

MyTable2

+id+myTable1

+id +myTable1

我的视图1

+id

最终哪个更好,因为这样我的模型中的 Null 值更少.

Which in the end is better because this way i have less Null values in my model.

推荐答案

在 EF 中,您可以使用数据库视图并将其映射到实体并引用它,就像处理表一样.对于代码优先过程,您必须在 Up 中创建 View 并将其放入迁移类的 Down 方法中:

In EF you can use a database views and map it to an entity and reference it just as you do with tables. For code first process you have to create the View in Up and drop it in Down methods from migration class:

public partial class AddView : DbMigration
  {
    public override void Up()
    {
      this.Sql(@"CREATE VIEW MyView1 AS ....");
    }
    public override void Down()
    {
        this.Sql(@"DROP VIEW MyView1....");
    }
  }

public long myTable2Id { get; set; }

[ForeignKey( "myTable2Id" )]
public virtual MyTable2 Table2 {get;set;}

这篇关于如何在 EF 6.1 CodeFirst 中的视图上添加导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1159

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1060

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:799

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:885

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:925

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:871

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:854

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:825

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1690

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1296