是否有类似[Table()]的属性来创建一个类,该类映射到EF Code First中的视图? [英] Is there an attribute like [Table()] to create a class that maps to a view in EF Code First?
本文介绍了是否有类似[Table()]的属性来创建一个类,该类映射到EF Code First中的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Entity Framework Code First,并且正在尝试创建一个将映射到View的类。我知道如何对一个表进行操作,如下所示:
I am working with Entity Framework Code First, and am trying to create a class that will map to a View. I know how to do it for a table, like below:
[Table("FIL002")]
public class FIL002
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public Int64 Payer_Number { get; set; }
public string Payer_Name { get; set; }
}
但是,我找不到View的属性(例如 [Table(...)]
。有吗?
However, I could not find an attribute for View (Like [Table(...)]
. Is there one?
推荐答案
有没有直接的方法如代码中所述,但是您可以通过DbContext发送原始SQL 创建视图 命令,如下所示:
There is no direct approach as explained in code-first but you can you DbContext to send raw SQL 'create view' command like this :
public class MyInitializer : CreateDatabaseIfNotExists<MyContext>
{
protected override void Seed(MyContext context)
{
query = "CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH <view_attribute> [ ,...n ] ]
AS select_statement
[ WITH CHECK OPTION ]
[ ; ]
<view_attribute> ::=
{
[ ENCRYPTION ]
[ SCHEMABINDING ]
[ VIEW_METADATA ]
} ";
context.Database.SqlCommand(query);
}
}
这篇关于是否有类似[Table()]的属性来创建一个类,该类映射到EF Code First中的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文