How can I use the database view in entity framework code first,
如何在实体框架代码中使用数据库视图,
3 个解决方案
#1
72
If, like me, you are interested only in mapping entity coming from an other database (an erp in my case) to relate them to entities specific of your application, then you can use the views as you use a table (map the view in the same way!). Obviously, if you try to update that entities, you will get an exception if the view is not updatable. The procedure is the same as in the case of normal (based on a table) entities:
如果像我一样,您只对来自其他数据库(在我的例子中是erp)的实体与应用程序特定的实体关联感兴趣,那么您可以在使用表时使用视图(以同样的方式映射视图!)显然,如果您试图更新该实体,那么如果视图不可更新,您将得到一个异常。程序与正常实体(基于表格)的程序相同:
- Create a POCO class for the view; for example FooView
- 为视图创建POCO类;例如FooView
- Add the DbSet property in the DbContext class
- 在DbContext类中添加DbSet属性
-
Use a FooViewConfiguration file to set a different name for the view (using ToTable("Foo"); in the constructor) or to set particular properties
使用FooViewConfiguration文件为视图设置不同的名称(使用ToTable(“Foo”);在构造函数中)或设置特定的属性
public class FooViewConfiguration : EntityTypeConfiguration<FooView> { public FooViewConfiguration() { this.HasKey(t => t.Id); this.ToTable("myView"); } }
-
Add the FooViewConfiguration file to the modelBuilder, for example ovveriding the OnModelCreating method of the Context:
将FooViewConfiguration文件添加到modelBuilder中,例如,对上下文的onmodelcreation方法进行ovveriding:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new FooViewConfiguration ()); }
#2
8
If all you want is a bunch of de-normalized objects, then you might just created a public get-only IQueryable<TDenormolized>
property in your DbContext
class.
如果您想要的只是一堆非规范化对象,那么您可以在DbContext类中创建一个仅用于公共get的IQueryable< tdenormoated>属性。
In the get
you return a Linq result to project the de-normoalized values into your de-normalized objects. This might be better than writing a DB View because you are programming, you are not limited by only using select
statements. Also it's compile time type safe.
在get中,返回一个Linq结果,将非规范化值投射到非规范化对象中。这可能比编写DB视图要好,因为您正在编程,不受只使用select语句的限制。它也是编译时类型安全的。
Just be careful not trigger enumerations like ToList()
calls, that will break the deferred query and you may end up with getting a million records back from the database and filter them on your application server.
只是要小心不要触发像ToList()调用这样的枚举,这会破坏延迟查询,最终您可能会从数据库获得100万条记录,并在应用服务器上对它们进行过滤。
I don't know if this is the right way, but I tried and it works for me.
我不知道这是不是正确的方法,但我试过了,它对我有效。
#3
5
This may be an update but to use views with EF Code first simply add [Table("NameOfView")] to the top of the class and all should work right without having to go through all the hoops everyone else is going through. Also you will have to report one of the columns as a [key] column. Here is my sample code below to implement it.
这可能是一个更新,但是使用EF代码的视图首先简单地将[表(“NameOfView”)添加到类的顶部,并且所有的操作都应该是正确的,而不需要遍历所有其他人正在经历的所有操作。您还必须将其中一个列报告为[key]列。下面是我的示例代码来实现它。
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SomeProject.Data
{
[Table("SomeView")]
public class SomeView
{
[Key]
public int NameID { get; set; }
public string Name { get; set; }
}
}
And here is what the context looks like
这是context的样子。
using System.Data.Entity;
namespace SomeProject.Data
{
public class DatabaseContext : DbContext
{
public DbSet<SomeView> SomeViews { get; set; }
}
}
#1
72
If, like me, you are interested only in mapping entity coming from an other database (an erp in my case) to relate them to entities specific of your application, then you can use the views as you use a table (map the view in the same way!). Obviously, if you try to update that entities, you will get an exception if the view is not updatable. The procedure is the same as in the case of normal (based on a table) entities:
如果像我一样,您只对来自其他数据库(在我的例子中是erp)的实体与应用程序特定的实体关联感兴趣,那么您可以在使用表时使用视图(以同样的方式映射视图!)显然,如果您试图更新该实体,那么如果视图不可更新,您将得到一个异常。程序与正常实体(基于表格)的程序相同:
- Create a POCO class for the view; for example FooView
- 为视图创建POCO类;例如FooView
- Add the DbSet property in the DbContext class
- 在DbContext类中添加DbSet属性
-
Use a FooViewConfiguration file to set a different name for the view (using ToTable("Foo"); in the constructor) or to set particular properties
使用FooViewConfiguration文件为视图设置不同的名称(使用ToTable(“Foo”);在构造函数中)或设置特定的属性
public class FooViewConfiguration : EntityTypeConfiguration<FooView> { public FooViewConfiguration() { this.HasKey(t => t.Id); this.ToTable("myView"); } }
-
Add the FooViewConfiguration file to the modelBuilder, for example ovveriding the OnModelCreating method of the Context:
将FooViewConfiguration文件添加到modelBuilder中,例如,对上下文的onmodelcreation方法进行ovveriding:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new FooViewConfiguration ()); }
#2
8
If all you want is a bunch of de-normalized objects, then you might just created a public get-only IQueryable<TDenormolized>
property in your DbContext
class.
如果您想要的只是一堆非规范化对象,那么您可以在DbContext类中创建一个仅用于公共get的IQueryable< tdenormoated>属性。
In the get
you return a Linq result to project the de-normoalized values into your de-normalized objects. This might be better than writing a DB View because you are programming, you are not limited by only using select
statements. Also it's compile time type safe.
在get中,返回一个Linq结果,将非规范化值投射到非规范化对象中。这可能比编写DB视图要好,因为您正在编程,不受只使用select语句的限制。它也是编译时类型安全的。
Just be careful not trigger enumerations like ToList()
calls, that will break the deferred query and you may end up with getting a million records back from the database and filter them on your application server.
只是要小心不要触发像ToList()调用这样的枚举,这会破坏延迟查询,最终您可能会从数据库获得100万条记录,并在应用服务器上对它们进行过滤。
I don't know if this is the right way, but I tried and it works for me.
我不知道这是不是正确的方法,但我试过了,它对我有效。
#3
5
This may be an update but to use views with EF Code first simply add [Table("NameOfView")] to the top of the class and all should work right without having to go through all the hoops everyone else is going through. Also you will have to report one of the columns as a [key] column. Here is my sample code below to implement it.
这可能是一个更新,但是使用EF代码的视图首先简单地将[表(“NameOfView”)添加到类的顶部,并且所有的操作都应该是正确的,而不需要遍历所有其他人正在经历的所有操作。您还必须将其中一个列报告为[key]列。下面是我的示例代码来实现它。
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace SomeProject.Data
{
[Table("SomeView")]
public class SomeView
{
[Key]
public int NameID { get; set; }
public string Name { get; set; }
}
}
And here is what the context looks like
这是context的样子。
using System.Data.Entity;
namespace SomeProject.Data
{
public class DatabaseContext : DbContext
{
public DbSet<SomeView> SomeViews { get; set; }
}
}