2

I have specified the proper connection string in the web.config, but the database isn't creating nor it hits break point at seed method.

Code:

public class MusicStoreEntities:DbContext
{
    public DbSet<Genre> genres;

    public MusicStoreEntities()
        : base("name=MusicStoreConnection")
    {
        Database.SetInitializer(new Myinitialzer());
    }
}


public class Myinitialzer :CreateDatabaseIfNotExists<MusicStoreEntities>
{
    protected override void Seed(MusicStoreEntities context)
    {
        var genres = new List<Genre>
        {
            new Genre { Name = "Rock" },
            new Genre { Name = "Jazz" },
            new Genre { Name = "Metal" },

         };
     }
 }

Connection string:

 <add name="MusicStoreConnection" providerName="System.Data.SqlClient" connectionString="Data Source=WAQAR_DEV;Initial Catalog=PlanetWrox;Integrated Security=true;" />
CodeCaster
  • 147,647
  • 23
  • 218
  • 272

2 Answers2

1

setters and getters are also listed with dbset entities that is the only reason database wasnt creating,here to this specific code no migrations are needed as the database is not even generated.just only need to add

public DbSet<genres>genres{get;set;}

0

You need to add the genres in to the context and save it.

context.genres.AddRange(genres);
context.SaveChanges();

ALSO:

  • See this question and also this
  • You may need to add System.Data.Entity.CreateDatabaseIfNotExists to your initializer
  • Have you set up migrations, and are they auto?
  • You may need to run a migration
  • Are you getting any error messages
Community
  • 1
  • 1
Peter Smith
  • 5,528
  • 8
  • 51
  • 77
  • its still not working as i have two model classes but i mentioned one here for simplicity for which i used this syntax ForEach(a => context.Albums.Add(a) – waqar ahmed Dec 30 '15 at 05:39
  • thanx peter smith for your help for providing good resources to learn but i need ur favour to accept my answer.. – waqar ahmed Jan 05 '16 at 06:14