entity framework - EF code first telling me to do the migration for db object which is already is in db -


i working ef code first. have no tables in database. wrote class , when query class saw ef code first create tables in db when create sql server view in db , later map view code in c# & ef project , when try query view getting error message follows.

additional information: model backing 'testdbcontext' context has changed since database created. consider using code first migrations update database

i understand ef telling me migration if migrate ef create view in db again when view in db exist.

so tell me how inform ef view in db migration not required. please guide me. thanks

edit 1

first time database has no table. wrote classes 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 query customer below query ef create required tables in db behind curtains.

var bscustomer = (from cu in db.customer   (cu.customerid == 2)   select new   {       cu,       addresses = ad in cu.addresses           (ad.isdefault == true)           ct in ad.contacts           select ad,    }).tolist(); 

later create view in db , refer view in code 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 dbcontext below 1 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 moment try query view

using (var db = new testdbcontext()) {     var listmyviews = db.vwcustomers.tolist(); } 

the error additional information: model backing 'testdbcontext' context has changed since database created. consider using code first migrations update database

thanks

add new migration , migration code in (and down) method remove code tries create new table manually (call createtable method in , droptable in down). apply migration db , works perfectly.

unfortunately automatic migration generation not intelligent tool , 1 need manually specify how database should altered. in documentation ef migrations stated fine edit manually migrations code.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -