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;" />

2 Answers 2

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;}

Sign up to request clarification or add additional context in comments.

1 Comment

Good spot - it's the obvious things that are hardest to see
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

2 Comments

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)
thanx peter smith for your help for providing good resources to learn but i need ur favour to accept my answer..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.