3

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)

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Navid Kianfar
  • 29
  • 1
  • 5
  • Try this: http://stackoverflow.com/questions/20862807/mapping-database-views-to-ef-5-0-code-first-w-migrations/35876571#35876571 – Mr. DMX Mar 08 '16 at 19:52
  • Try my [solution](https://stackoverflow.com/a/62287959/5200896). It prevents migration generation for tables marked as views – giokoguashvili Jun 09 '20 at 17:04

2 Answers2

1

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
a''
  • 2,322
  • 2
  • 23
  • 34
  • maybe i dint give enough information; the migration is enabled and database is regenerating if model changes!!!! – Navid Kianfar Jun 29 '13 at 21:28
  • and about combination: i need them merged sth like this: `public class SearchResult { public SearchResult() { Query = null; Total = TotalPages = 0; Page = 1; Result = new List(); } public int Page { get; set; } public string Query { get; set; } public List Result { get; set; } public int Total { get; set; } public int TotalPages { get; set; } }` – Navid Kianfar Jun 29 '13 at 21:33
0

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>();
    }
}
Mr. DMX
  • 653
  • 2
  • 9
  • 20