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

查看:282
本文介绍了如何在EF 6.1 CodeFirst的视图中添加导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们来解释一下我的问题。






MyTable1



id



+ myTable2Id






MyTable2



+ id






MyView1



+ id



+ myTable2Id



< hr>

MyView1存在于这种情况下,来自MyTable1的数据。现在我想在我的MyTable2视图中从我的EF6.1代码第一个方法创建一个导航属性。



我知道这是可能从数据库的第一种方法,但也可以从代码优先的方法和如何?



编辑:



我在互联网上搜索一些,但由于View的含义很多,很难找到关于它的信息。



还有我尝试的代码中的方法,我总是收到一个错误,无法完成迁移。因为迁移尝试在视图中添加一个外键,这是不可能的。



EDIT2:



详细阐述我的解释。我想要以下面的方式处理代码:

  Guid table2Id = context.MyView1.FirstOrDefault()。MyTable2 。ID; 

EDIT3:



我会再多一点,看看我能否更好的解释我的问题。



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

  public virtual MyTable2 Table2 {get;设置;} 

EF将自动生成以下迁移:

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

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



无法创建视图MyView1上的索引,因为视图不是模式绑定 / p>

EDIT4:



借助于评论,迁移不是石头..而且是可变的做好了。



我使用以下fluentAPI:

  // Map one-零或一个关系
modelBuilder.Entity< MyTable2>()
.HasRequired(t => t.MyTable1)
.WithOptional(t => t.MyTable2);

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

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




MyTable1



+ id






MyTable2



+ id
+ myTable1






MyView1



+ id






哪个到底是更好的,因为这样我的模型中的空值越少

解决方案

在EF中,您可以使用数据库视图并将其映射到实体,并与表一样进行引用。
对于代码第一个进程,您必须创建向上查看,并将其从迁移类中的下降方法中删除:

  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;组; } 

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


Let's make a case to explain my problem.


MyTable1

+id

+myTable2Id


MyTable2

+id


MyView1

+id

+myTable2Id


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?

EDIT:

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.

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:

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:

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

Which on running update-database gives the following error :

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

EDIT4:

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

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);

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


MyTable1

+id


MyTable2

+id +myTable1


MyView1

+id


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

解决方案

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....");
    }
  }

EDIT:

public long myTable2Id { get; set; }

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

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

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