简体   繁体   English

ASP.NET MVC-数据库视图为DbSet吗?

[英]ASP.NET MVC - Database View as DbSet?

I'm having a hard time connecting to an existing view in the database. 我很难连接到数据库中的现有视图。 I've created the model and now I'm trying to add the DbSet to my existing context so I can query it. 我已经创建了模型,现在尝试将DbSet添加到现有上下文中,以便可以查询它。

I've tried the following, but every time I create a new EF migration, it tries to create the object as a table in the database. 我尝试了以下操作,但是每次创建新的EF迁移时,它都会尝试将对象创建为数据库中的表。

public DbSet<SurveyResponse> SurveyResponses { get; set; }

I tried the same declaration as an IQueryable which seemed to stop the creation of the table, but when I tried to access context.SurveyResponses, SurveyResponses was showing no data. 我尝试了与IQueryable相同的声明,该声明似乎停止了表的创建,但是当我尝试访问上下文时。SurveyResponses,SurveyResponses没有显示数据。

Any ideas? 有任何想法吗?

To stop EF creating the tables you can specify the ignore changes flag when adding a migration. 要停止EF创建表,可以在添加迁移时指定忽略更改标志。

add-migration name -ignorechanges

Entity Framework treats the view mappings the same as table mappings. 实体框架将视图映射与表映射相同。

Just remember not to update the view entity otherwise you will get an error! 请记住不要更新视图实体,否则会出现错误!

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

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