Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8682643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:49:51+00:00 2026-06-12T21:49:51+00:00

I’m pretty new to using Entity Framework, as well as to using ASP.NET MVC.

  • 0

I’m pretty new to using Entity Framework, as well as to using ASP.NET MVC. I’ve been able to complete a couple of tutorials on the ASP.NET MVC website and thought I would start my own project based on the MVC Music Store application.

I can’t tell where I went wrong (it all looks the same to me), but for some reason my seed data isn’t creating the database.

Item class:

namespace MySite.Models
{
    public class Item
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

Tag class:

namespace MySite.Models
{
    public class Tag
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

ItemTag class:

namespace MySite.Models
{
    public class ItemTag
    {
        public int Id { get; set; }
        public int ItemId { get; set; }
        public int TagId { get; set; }
    }
}

MySiteEntities class:

namespace MySite.Models
{
    public class MySiteEntities : DbContext
    {
        public DbSet<Item> Items { get; set; }
        public DbSet<Tag> Tags { get; set; }
        public DbSet<ItemTag> ItemTags { get; set; }
    }
}

Global.asax class:

namespace MySite
{
    public class MvcApplication : System.Web.HttpApplication
    {
        ...

        protected void Application_Start()
        {
            System.Data.Entity.Database.SetInitializer(new MySite.Models.SampleData());

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

Web.config file:

<configuration>
  ...

  <connectionStrings>
    <add name="MySiteEntities"
      connectionString="Data Source=|DataDirectory|MySite.sdf"
      providerName="System.Data.SqlServerCe.4.0" />
  </connectionStrings>
</configuration>

SampleData class:

namespace MySite.Models
{
    public class SampleData : DropCreateDatabaseIfModelChanges<MySiteEntities>
    {
        protected override void Seed(MySiteEntities context)
        {
            new List<Item>
            {
                new Item { Id = 1, Name = "Bob Jones" },
                new Item { Id = 2, Name = "George Smith" },
                new Item { Id = 3, Name = "Boys and Girls" },
                new Item { Id = 4, Name = "The President's Hair" },
                new Item { Id = 5, Name = "Invaders From Mars" },
                new Item { Id = 6, Name = "Tank Shooter" },
                new Item { Id = 7, Name = "Stew's Blog" },
                new Item { Id = 8, Name = "Social Mania" }
            }.ForEach(i => context.Items.Add(i));

            new List<Tag>
            {
                new Tag { Id = 1, Name = "Author" },
                new Tag { Id = 2, Name = "Movie" },
                new Tag { Id = 3, Name = "Video Game" },
                new Tag { Id = 4, Name = "Website" }
            }.ForEach(t => context.Tags.Add(t));

            new List<ItemTag>
            {
                new ItemTag { Id = 1, ItemId = 1, TagId = 1 },
                new ItemTag { Id = 2, ItemId = 2, TagId = 1 },
                new ItemTag { Id = 3, ItemId = 3, TagId = 2 },
                new ItemTag { Id = 4, ItemId = 4, TagId = 2 },
                new ItemTag { Id = 5, ItemId = 5, TagId = 3 },
                new ItemTag { Id = 6, ItemId = 6, TagId = 3 },
                new ItemTag { Id = 7, ItemId = 7, TagId = 4 },
                new ItemTag { Id = 8, ItemId = 8, TagId = 4 }
            }.ForEach(x => context.ItemTags.Add(x));
        }
    }
}

That’s pretty much everything. I did delete the database file at one point. Shouldn’t it get created again when I build the solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:49:53+00:00Added an answer on June 12, 2026 at 9:49 pm

    Reason: This is because you are using “DropCreateDatabaseIfModelChanges“.

    As it indicates if your model doesn’t change then it wont create the database (or if the DB is already there then it wont add your populated data).

    Solution: You want to use “CreateDatabaseIfNotExists” instead.

    Alternative: If you’re DB is already created and you just want to add initial data then use “DropCreateDatabaseAlways” HOWEVER you must realise this will recreate the DB from scratch every time an application restarts. So only use this when your DB is already created and you don’t want to change the model (and you don’t care about losing the data already in the DB) AND then change it to one of the other two options.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.