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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:18:46+00:00 2026-05-25T19:18:46+00:00

I’m using SqlServerCompact, Entity framework with code-first and I’m generating the connection string dinamically

  • 0

I’m using SqlServerCompact, Entity framework with code-first and I’m generating the connection string dinamically because the database it’s generated when the user requests it.

I followed suggestions from this question: How do I programmatically set the connection string for Entity-Framework Code-First? however, I still have problems, infact I get an exception complaining that the schema in the model is not the same as used in code.

That’s obvius however, I want the schema to be generated from code, so I really can’t understand why this happen

Can someone help me?Here is the code used:

NerdDinners.cs

class NerdDinners : DbContext
{
    public static string CreateConnectionString(string dbPath = @"|DataDirectory|\NerdDinners.sdf")
    {
        SqlCeConnectionStringBuilder sqlConnection = new SqlCeConnectionStringBuilder();
        sqlConnection.Password = "9023fase93";
        sqlConnection.DataSource = dbPath;

        EntityConnectionStringBuilder connection = new EntityConnectionStringBuilder();
        connection.Metadata = @"res://*/NerdDinnersModel.csdl|res://*/NerdDinnersModel.ssdl|res://*/NerdDinnersModel.msl";
        connection.Provider = "System.Data.SqlServerCe.3.5";
        connection.ProviderConnectionString = sqlConnection.ToString();

        return connection.ToString();
    }

    public NerdDinners() : base() { }
    public NerdDinners(string nameOrConnectionString) : base(nameOrConnectionString) { }

    public DbSet<Dinner> Dinners { get; set; }
    public DbSet<RSVP> RSVPs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer<NerdDinners>(new NerdDinnersInitializer());
        Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe");

        base.OnModelCreating(modelBuilder);
    }
}

Program.cs

class Program
{
    static void Main(string[] args)
    {
        NerdDinners nerdDinner = new NerdDinners(NerdDinners.CreateConnectionString());
        Console.WriteLine("Inizio test");

        var res = from d in nerdDinner.Dinners
                  select d;

        Console.WriteLine("Dinners:");
        foreach (var item in res)
            Console.WriteLine(item.ToString());
        Console.WriteLine("End");

        // Usato per terminazione
        Console.ReadKey();
    }
}

An this is my solution situation (an image):

Solution rapresentation

EDIT 1:

The error is a MetadataException, it complains about invalid schema

EDIT 2:

NerdDinnersInitializer.cs

class NerdDinnersInitializer : DropCreateDatabaseAlways<NerdDinners>
{
    public NerdDinnersInitializer() : base() { }

    protected override void Seed(NerdDinners context)
    {
        var toadd = new List<Dinner>
        {
            new Dinner
            {
                Address = "bla",
                DinnerID = 0,
                EventDate = DateTime.Parse("2010-12-03"),
                HostedBy = "mclink",
                Title = "Notitle",
            },
            new Dinner
            {
                Address = "Fiwe",
                DinnerID = 0,
                EventDate = DateTime.Parse("2011-12-03"),
                HostedBy = "ngi",
                Title = "Bah",
            },
        };

        toadd.ForEach(item => context.Dinners.Add(item));

        context.SaveChanges();

        base.Seed(context);
    }
}
  • 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-05-25T19:18:47+00:00Added an answer on May 25, 2026 at 7:18 pm

    I found the answer with deep google searching.
    I understood that I misunderstood how code first works, infact you can’t create a database you want to connect to if you are using code-first.

    I still don’t understand if you can save in a chosen place the database generated with code-first (I think you can’t).

    However, if you want to connect to an existing database file you have to use model-first or database-first approach you can’t use code first

    This is what I understood, hopefully I’m not wrong.
    This question explains it better: Why does Entity Framework Code-First (with an existing DB) keep trying get data from an EdmMetadata table?

    EDIT 1:
    It looks like I was wrong, is possible, but you need SQL Server compact 4.0 and not 3.5 (I was having problems with it), also you need vs 2010 and entity framework 4.1.
    Then, just follow the answer to this question to create dinamically the string: Is possible to create a database (Sql Server compact) on a given path if we use Entity framework with code-first approach?

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
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 want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
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

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.