c# - Context and Context Initializer Not Running -


i'm attempting code first development on mvc site , although global.ascx file running code make new initializer, initializer isn't being run. i've placed breakpoint on both start of context , initializer , both not being hit.

global.ascx

database.setinitializer<abstractcontext>(new abstractcontextinitializer()); 

context:

using system; using system.collections.generic; using system.data.entity; using system.linq; using system.web;  namespace abstractthinking2015.models {     public class abstractcontext : dbcontext     {         public dbset<blogmodel> blogs { get; set; }     }          } 

initializer:

using system; using system.collections.generic; using system.data.entity; using system.linq; using system.web;  namespace abstractthinking2015.models {     public class abstractcontextinitializer : dropcreatedatabasealways<abstractcontext>     {         protected override void seed(abstractcontext context)         {             context.blogs.add(                 new blogmodel() { title = "test title",                                   date = datetime.now,                                   user = "liane",                                   category = "test",                                   post = "lorem ipsum dolor sit amet, consectetur adipiscing elit. phasellus rhoncus tristique leo et vehicula. integer eu imperdiet orci, sit amet convallis ipsum. sed in commodo urna. sed venenatis neque augue, et faucibus purus aliquet eget. suspendisse nec sapien nec justo ullamcorper rutrum. praesent maximus nulla eget rhoncus scelerisque. vivamus @ felis porta, placerat augue non, interdum dui."                                  });                 context.savechanges();         }     } } 

and in case, here's model:

using system; using system.collections.generic; using system.componentmodel; using system.componentmodel.dataannotations; using system.linq; using system.web;  namespace abstractthinking2015.models {     public class blogmodel     {         //public blogmodel()         //{         //    date = datetime.now;         //    user = "liane";         //}          public int blogmodelid { get; set; }         [displayname("blog title")]         [required(errormessage = "your blog must have title")]         public string title { get; set; }         [displayname("blog date")]         [required(errormessage = "your blog must have date")]         public datetime date { get; set; }         [displayname("blog user")]         [required(errormessage = "your blog must have user")]         public string user { get; set; }         [displayname("blog category")]         [required(errormessage = "your blog must have category")]         public string category { get; set; }         [displayname("blog post")]         [required(errormessage = "your blog must have post")]         [datatype(datatype.multilinetext)]         public string post { get; set; }     } } 

you can put call initializer inside dbcontext class, so:

public class auctionsdatacontext     inherits dbcontext      public property auctions dbset(of auction)     public property bids dbset(of bid)      shared sub new()         database.setinitializer(new dropcreatedatabaseifmodelchanges(of auctionsdatacontext))     end sub  end class 

note constructor needs defined static (c#) or shared (vb) method.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -