Entity Framework 6 and SQL Server CE with code-first

148 Views Asked by At

I'm using Entity Framework (code-first approach) and a SQL Server CE database.

I have the problem that the SQL Server CE database is not created and it seems that the app.config file isn't used because there is a false connection string inside the connection (sqllocaldb).

Projects:

  • SaltMgr (WPF app which calls SaltMgr.Data)

Code in MainWindow only for test call:

        var rep = new WiegedatenRepository();
        var xx = rep.List();

SaltMgr.Data - DbContext:

using SaltMgr.Data.Model;
using System;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity;
using System.Data.SqlServerCe;

namespace SaltMgr.Data.DAL
{
    public class RepositoryContext : DbContext
    {
        static RepositoryContext()
        {
            try
            {
                // Database initialize
                Database.SetInitializer<RepositoryContext>(new DbInitializer());

                using (var db = new RepositoryContext())
                {
                    db.Database.Initialize(false);
                    db.Database.CreateIfNotExists();
                }
            }
            catch(Exception)
            {
                throw;
            }
        }

        public DbSet<Wiegedaten> Wiegedaten { get; set; }
    }

    class DbInitializer : CreateDatabaseIfNotExists<RepositoryContext>
    {
    }
}

and the app.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </configSections>
    
    <entityFramework>
        <providers>
            <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="System.Data.SqlServerCe.4.0"/>
            </parameters>
        </defaultConnectionFactory>
    </entityFramework>
    
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0"/>
            <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>
        </DbProviderFactories>
    </system.data>
    
    <connectionStrings>
        <add name="SaltMgr.Data.DAL.RepositoryContext" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=C:\Sourcecode\SaltMgr\Database\SaltMgrDatabase.sdf"/>
    </connectionStrings>
    
</configuration>

And the Wiegedaten model class:

using System;
using System.ComponentModel.DataAnnotations;

namespace SaltMgr.Data.Model
{
    public class Wiegedaten
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public int LaufendeNummer { get; set; }

        [Required]
        public DateTime Datum { get; set; }

        [Required]
        [StringLength(100)]
        public string Kundenname { get; set; }

        [Required]
        [StringLength(100)]
        public string Entnehmername { get; set; }

        [Required]
        public int Tara { get; set; }

        [Required]
        public int Brutto { get; set; }

        [Required]
        public int Netto { get; set; }

        [Required]
        public int AlibiNummer { get; set; }
    }
}

Does someone have a solution? Why is this not working?

Project download

1

There are 1 best solutions below

0
ErikEJ On

You must move the configuration (app config) content to your executable project, it has no effect on the other projects. So your database is correctly created in your LocalDB instance! Once you do that, SQL provider kicks in as expected.