谁知道.Net Core EF中视图怎么映射……

卧槽这是我的昵称吗 2017-04-01 12:55:08
跟着微软的.Net Core MVC的教程走,里面只有表的映射,视图怎么映射啊?

网上找了点解决方法 都不管用

比如:


0
down vote
I'm actually working with Entity Framework "Code First" and views, the way I do it is like this:

1) Create a class

[Table("view_name_on_database")]
public class ViewClassName {
// View columns mapping
public int Id {get; set;}
public string Name {get; set;}
// And a few more...
}
2) Add the class to the context

public class ContextName : DbContext {
// Tables
public DbSet<SomeTableClassHere> ATable { get; set; }
// And other tables...

// Views
public DbSet<ViewClassName> ViewContextName { get; set; }

// This lines help me during "update-database" command
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
// Remove comments before "update-database" command and
// comment this line again after "update-database", otherwise
// you will not be able to query the view from the context.
// Ignore the creation of a table named "view_name_on_database"
modelBuilder.Ignore<ViewClassName>();
}
}


直接就报错了
An unhandled exception occurred while processing the request.

ArgumentNullException: Value cannot be null.
Parameter name: entityType
Microsoft.EntityFrameworkCore.Utilities.Check.NotNull<T>(T value, string parameterName)
...全文
1410 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
x80819092 2017-05-09
  • 打赏
  • 举报
回复
引用 4 楼 a18732923 的回复:
楼主找到什么办法可以自动构建Dbcontext带视图的吗?不然视图还要手动添加
你如果是从空项目建的话,有个小bug,把controller关掉再打开就能加了
总是不会飞 2017-05-09
  • 打赏
  • 举报
回复
楼主找到什么办法可以自动构建Dbcontext带视图的吗?不然视图还要手动添加
x80819092 2017-04-18
  • 打赏
  • 举报
回复
你好,你建的.net core项目是英文界面还是中文啊,我剪出来是英文的,怎么改
guanyelong 2017-04-18
  • 打赏
  • 举报
回复
先顶后看年薪百万
  • 打赏
  • 举报
回复
解决了。 不要加 modelBuilder.Ignore<ViewClassName>(); 这句。

62,244

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧