【问题标题】:Entity Framework 6 and SQL Server CE with code-firstEntity Framework 6 和 SQL Server CE,代码优先
【发布时间】:2021-05-04 04:41:18
【问题描述】:

我正在使用实体框架(代码优先方法)和 SQL Server CE 数据库。

我的问题是没有创建 SQL Server CE 数据库,并且似乎没有使用 app.config 文件,因为连接 (sqllocaldb) 中存在错误的连接字符串。

项目:

  • SaltMgr(调用SaltMgr.Data的WPF应用)

MainWindow 中的代码仅用于测试调用:

        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>
    {
    }
}

还有app.config 文件:

<?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>

还有Wiegedaten 模型类:

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; }
    }
}

有人有解决方案吗?为什么这不起作用?

Project download

【问题讨论】:

  • 是什么让你觉得数据库没有创建?
  • 你检查过 bin/debug 文件夹了吗?
  • bin/debug 为空,因为输出 driectory 设置为 ../output/debug 或 release。在这些文件夹中也没什么。在应用配置中定义了路径:connectionString="Data Source=C:\Sourcecode\SaltMgr\Database\SaltMgrDatabase.sdf" 并且这个文件夹是空的。
  • 添加项目下载...
  • 你有什么错误吗??

标签: entity-framework code-first sql-server-ce-4


【解决方案1】:

您必须将配置(app config)内容移动到您的可执行项目中,它对其他项目没有影响。 因此,您的数据库已在 LocalDB 实例中正确创建! 一旦你这样做了,SQL 提供程序就会按预期启动。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多