2

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 Answer 1

2

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);
     }
}
Sign up to request clarification or add additional context in comments.

11 Comments

Yes, I missed it, thanks. But there is one another problem. When I delete a .sdf file and run an my application the .sdf data file is not created and should be, isn't it? It is connected to my original question how this initalization process works.
Yes... it should be created automatically after you restart the app and it detects the .sdf doesn't exist anymore. Generally this file is created within the \App_Data folder.
But in my case the .sdf file is not created. Is there some necessity to commit changes or somethink like it? Where should be problem? Thanks for any advice, I googled for one hour and I have no idea where is problem. Maybe is a problem with my ConnectionString - I defined file location as full path (many tutorials use another syntax).
I mentioned connection string in my question above.
My context class FitnessCenterContext is inside the namespace FitnessCenter.Model. It's a separate class library that I use. If you're not familiar with namespace, check this: msdn.microsoft.com/en-us/library/0d941h9d.aspx. Can you please tell me what you're using: SQL Server Express, LocalDB, etc... to store the data. Did you try placing the code inside Application_Start() instead of Session_Start()?
|

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.