实体框架代码第一和视图映射 [英] entity framework code first and view mapping
问题描述
在模型创建以阻止EF创建与表相同名称的表(VIEW_SEARCH)我做了:
保护覆盖void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore< View_Search>();
}
任何方式的应用程序工作正常,直到您尝试从视图中获取数据,然后BANG ...
自创建数据库以来,支持SearchContext上下文的模型已更改。考虑使用代码优先迁移来更新数据库( http://go.microsoft.com/fwlink/ ?LinkId = 238269 )
这个错误只是说你有在您的模型文件中与您的数据库中的内容不一致。
要使其一致,请访问Package Manager控制台并键入Enable-Migrations,然后添加迁移yourMigrationName和Update-Database。错误应该消失。
如果要组合来自3个表的数据,您可以简单地创建一个ViewModel。
假设你有3种型号:Book,Author,BookStore,并且您想在一个视图中拥有所有信息。您创建ViewModel
public class MyViewModel
{
public Book myBook {get; set;}
public作者myAuthor {get; set;}
public BookStore myBookStore {get; set;}
}
然后你添加在你的all-in-one的顶部-view
@model myNamespace.MyViewModel
并访问项目,如
Model.Book.title
Model.Author .name
Model.BookStore.isClosed
I have a application using code first; in search section I have to gather information from 3 tables and their related tables so I made a view; and since there is no syntax for code first to create view (I think so; please let me know if I'm wrong) I used pure SQL script; on model creating to prevent EF to create a table with same name as table (VIEW_SEARCH) I did :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Ignore<View_Search>();
}
any ways application works fine until you try to get data from the view then BANG...
The model backing the 'SearchContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269)
This error simply says that what you have in your model file is inconsistent with what you have in your database. To make it consistent go to Package Manager Console and type Enable-Migrations, then Add-Migration yourMigrationName and Update-Database. The error should disappear.
If you want to combine data from 3 tables you can simply create a ViewModel.
Let's say you have 3 models: Book, Author, BookStore and you want to have all information in one view. You create ViewModel
public class MyViewModel
{
public Book myBook {get; set;}
public Author myAuthor {get; set;}
public BookStore myBookStore {get; set;}
}
Then you add at the top of your all-in-one-view
@model myNamespace.MyViewModel
and access items like
Model.Book.title
Model.Author.name
Model.BookStore.isClosed
这篇关于实体框架代码第一和视图映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!