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

こ雲淡風輕ζ 提交于 2019-12-01 18:45:09

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;}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!