c# - Can not seed data with EF Code First -


i found problem, solution @ comments.

i can create tables , diagram can not seed data table.

1.i installed ef nuget.

2.from pm console wrote enable-migrations –enableautomaticmigrations.

model in all.model class library , and context methods in all.dal class library did not understand doing wrong can me?

this context code:

using all.model;     namespace all.dal    {     public class alldb : dbcontext     {     public alldb()     {         database.connection.connectionstring = "server=seuphoria;database=alldb;uid=sa;pwd=123;";     }      public dbset<category> categories { get; set; }     public dbset<comment> comments { get; set; }     public dbset<line> lines { get; set; }     public dbset<user> users { get; set; }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         database.setinitializer<alldb>(new dbstrategy());         modelbuilder.entity<category>().property(c => c.name).isrequired();         modelbuilder.entity<comment>().property(c => c.letter).isrequired();     } } 

}

and strategy code:

using all.model;  namespace all.dal {    public class dbstrategy : dropcreatedatabaseifmodelchanges<alldb>   {       protected override void seed(alldb context)     {         list<category> categorydefault = new list<category>          {             new category { name="organic", upid = 0 },             new category { name="object", upid=0},             new category { name="time",upid=0},         };          foreach (category item in categorydefault)         {           context.categories.add(item);         } context.users.add(new user { name = "sss" });      }  } 

}

this category class:

 public class category : standart   {     public int upid { get; set; }     public string name { get; set; }     public int lineid { get; set; }     public virtual list<line> lines { get; set; }  } 

you adding items db context not committing changes calling savechanges() on them. add 1 line:

protected override void seed(alldb context) {     list<category> categorydefault = new list<category>      {         new category { name="organic", upid = 0 },         new category { name="object", upid=0},         new category { name="time",upid=0},     };      foreach (category item in categorydefault)     {       context.categories.add(item);     }     context.users.add(new user { name = "sss" });     context.savechanges(); // make sure save! } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -