i am working with EF code first. so initially i have no tables in database. so i wrote some class and when query those class then i saw EF code first create those tables in db but when i create sql server view in db and later map that view with my code in c# & EF project and when i try to query that view then i was getting error message as follows.
Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
i understand that EF is telling me to do the migration but if i migrate then EF will create that view in db again when the view is in db already exist.
so tell me how could i inform EF that my view is already is in db so migration is not required. please guide me. thanks
EDIT 1
first time my database has no table. so i wrote some classes like below one.
public class CustomerBase { public int CustomerID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public string Phone { get; set; } public string Fax { get; set; } } public class Customer : CustomerBase { public virtual List<Addresses> Addresses { get; set; } } public class Addresses { [Key] public int AddressID { get; set; } public string Address1 { get; set; } public string Address2 { get; set; } public bool IsDefault { get; set; } public virtual List<Contacts> Contacts { get; set; } public int CustomerID { get; set; } public virtual Customer Customer { get; set; } } public class Contacts { [Key] public int ContactID { get; set; } public string Phone { get; set; } public string Fax { get; set; } public bool IsDefault { get; set; } public int AddressID { get; set; } public virtual Addresses Customer { get; set; } } public class TestDBContext : DbContext { public TestDBContext() : base("name=TestDBContext") { } public DbSet<Customer> Customer { get; set; } public DbSet<Addresses> Addresses { get; set; } public DbSet<Contacts> Contacts { get; set; } } when i query the customer like below query then EF create all required tables in db behind the curtains.
var bsCustomer = (from cu in db.Customer where (cu.CustomerID == 2) select new { cu, Addresses = from ad in cu.Addresses where (ad.IsDefault == true) from ct in ad.Contacts select ad, }).ToList(); later i create a view in db and refer that view in code like below one.
public partial class vwCustomer { [Key] public int CustomerID { get; set; } public string FirstName { get; set; } } public class vwCustomerConfiguration : EntityTypeConfiguration<vwCustomer> { public vwCustomerConfiguration() { this.HasKey(t => t.CustomerID); this.ToTable("vwCustomers"); } } so now my DbContext look like below one with view class reference
public class TestDBContext : DbContext { public TestDBContext() : base("name=TestDBContext") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Configurations.Add(new vwCustomerConfiguration()); } public DbSet<Customer> Customer { get; set; } public DbSet<Addresses> Addresses { get; set; } public DbSet<Contacts> Contacts { get; set; } public virtual DbSet<vwCustomer> vwCustomers { get; set; } } Error occur the moment i try to query the view
using (var db = new TestDBContext()) { var listMyViews = db.vwCustomers.ToList(); } the error was Additional information: The model backing the 'TestDBContext' context has changed since the database was created. Consider using Code First Migrations to update the database
thanks