如何在 EF 6.1 CodeFirst 中的视图上添加导航属性 [英] How can you add a navigation property on a view in EF 6.1 CodeFirst

查看:18
本文介绍了如何在 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 中的视图上添加导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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