简体   繁体   English

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

[英]How can you add a navigation property on a view in EF 6.1 CodeFirst

Let's make a case to explain my problem. 让我们来解释一下我的问题。


MyTable1 MYTABLE1

+id + ID

+myTable2Id + myTable2Id


MyTable2 MyTable2

+id + ID


MyView1 MyView1

+id + ID

+myTable2Id + myTable2Id


MyView1 exists in the case, from data from the MyTable1. MyView1存在于案例中,来自MyTable1的数据。 Now i want to create a Navigation property from my EF6.1 Code first approach in my View to MyTable2. 现在我想在我的View to MyTable2中从我的EF6.1 Code第一种方法创建一个Navigation属性。

I know that it was possible from the database first approach, but is it also possible from the code-first approach and how? 我知道从数据库第一种方法可以实现,但也可以从代码优先方法中获得,如何实现?

EDIT: 编辑:

I search some on internet, but due many meanings of the word View, it's very hard to find information on it. 我在互联网上搜索一些,但由于View这个词有很多含义,很难找到它的信息。

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. 因为迁移尝试向视图添加外键,这是不可能的。

EDIT2: EDIT2:

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;

EDIT3: EDIT3:

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 will automaticly generate the following migration: EF将自动生成以下迁移:

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

Which on running update-database gives the following error : 在运行update-database时会出现以下错误:

"Cannot create index on view 'MyView1' because the view is not schema bound" “无法在视图'MyView1'上创建索引,因为视图不是模式绑定的”

EDIT4: EDIT4:

With help of the comment that the migration aren't of stone.. and are changeable i made it. 在评论的帮助下,迁移不是一成不变的,并且是可变的,我做到了。

I used the following fluentAPI: 我使用了以下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);

And changing my tables to this: (The FK to the MyTable2 and removed from the view) 并将我的表更改为:( FK到MyTable2并从视图中删除)


MyTable1 MYTABLE1

+id + ID


MyTable2 MyTable2

+id +myTable1 + id + myTable1


MyView1 MyView1

+id + ID


Which in the end is better because this way i have less Null values in my model. 最终哪个更好,因为这样我的模型中的Null值更少。

In EF you can use a database views and map it to an entity and reference it just as you do with tables. 在EF中,您可以使用数据库视图并将其映射到实体,并像对待表一样引用它。 For code first process you have to create the View in Up and drop it in Down methods from migration class: 对于代码优先进程,您必须在Up中创建View并将其从Migration类中删除Down方法:

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

EDIT: 编辑:

public long myTable2Id { get; set; }

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

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

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