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 9066607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:45:52+00:00 2026-06-16T16:45:52+00:00

I have problem with initializing data into SQL Server Compact .sdf data file in

  • 0

I have problem with initializing data into SQL Server Compact .sdf data file in .NET web application.

I have a data initialization class.

namespace R10491.Models
{
    public class SampleData : DropCreateDatabaseAlways<LibraryEntities>
    {
        protected override void Seed(LibraryEntities context)
        {
            var categories = new List<Category>
            {
                new Category{Id=1, Name="Sci-fi"}
            };

        }
    }
}

(for testing purposes I use DropCreateDatabaseAlways instead of DropCreateDatabaseIfModelChanges)

This initializer class I call in the Global.asax.cs file:

protected void Session_Start()
{
   System.Data.Entity.Database.SetInitializer(new R10491.Models.SampleData());
}

(again for testing purposes I call it on every session start).

My connection string definition:

  <connectionStrings>
    <add name="LibraryEntities"
     connectionString="Data Source=C:\Users\Administrator\Documents\Visual Studio 2012\Projects\2OBOP3_KU1\R10491\App_Data\R10491_library.sdf;"
     providerName="System.Data.SqlServerCe.4.0"/>
  </connectionStrings>

But the initialization doesn’t work – tables defined in SampleData class are not created nor data are initialized.

  • 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-16T16:45:53+00:00Added an answer on June 16, 2026 at 4:45 pm

    Looks like you’re forgetting to add the just created Category to the DB table. If you don’t add it to the context‘s table, Entity Framework won’t see anything… So you must do something like this:

    protected override void Seed(LibraryEntities context)
    {
        var categories = new List<Category>
        {
            new Category{Id=1, Name="Sci-fi"}
        };
    
        foreach(Category c in categories)
        {
            context.Categories.Add(c)
        }
    
        // Call the Save method in the Context
        context.SaveChanges();  
    }
    

    For the DataSource problem, try this modified connection string:

    <add name="LibraryEntities"
         connectionString="DataSource=|DataDirectory|R10491_library.sdf"
         providerName="System.Data.SqlServerCe.4.0" />
    

    In one of my projects I have this connection string:

    <add name="FitnessCenterContext"
         connectionString="DataSource=|DataDirectory|FitnessCenter.Model.FitnessCenterContext.sdf"
         providerName="System.Data.SqlServerCe.4.0" />
    

    Note above that the database name matches the namespace and Context name.


    I also use Application_Start() to call the SetInitializer method in Global.asax.cs file. I see that you’re calling it inside Session_Start(). Maybe this is the problem… Change your code to:

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

    You can also try calling the Initialize method:

    protected void Application_Start()
    {
         System.Data.Entity.Database.SetInitializer(new R10491.Models.SampleData());
    
         using (var context = new LibraryEntities())
         {
             context.Database.Initialize(true);
         }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem while loading data into html select when users press or click
I have a problem with my Mojarra 2.1.6 web-application, I'm developing it using @ViewScoped
I have a class that retrieves data from core data and stores it into
I have a problem initializing an array whose size is defined as extern const.
I have problem with show or hide form in Window Form Application. I start
I have problem SIMILAR to preventing form data reposting, but not quite the same
I have a very odd problem. A class property is mysteriously reset between method
I have a web application working with sqlite database. My version of sqlite is
I have C# application we've been coding in ADO.NET. I've been using the IDbCommand
I have a question today involving the StreamReader class. Specifically initializing this class using

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.