【问题标题】:file based SQLite with EF6 NOT NULL Constraint failed on Insert带有 EF6 NOT NULL 约束的基于文件的 SQLite 在插入时失败
【发布时间】:2018-09-16 18:27:34
【问题描述】:

我想创建一个仅可与 .exe 一起使用的程序,因此我需要从我的程序内部创建一个基于文件的数据库。我正在尝试将 SQLite 与实体框架一起使用,因此我设置了以下类:

程序.cs

class Program
{
    static void Main(string[] args)
    {
        using (MyContext context = new MyContext())
        {
            context.Documents.Add(new Document() { Id = 1, CategoryId = 2, Description = "test", Keywords = "test,TEST,", Text = "TEST test Test" });
            Console.WriteLine(context.Documents.Single(x => x.Id == 1).Text);
            Console.ReadKey();
        }
    }
}

class MyContext : DbContext
{
    public DbSet<Document> Documents { get; set; }
}

class Document
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
    public string Text { get; set; }
    public string Keywords { get; set; }
    public string Description { get; set; }
}

在运行代码时,它会抛出这个异常:

Unable to complete operation. The supplied SqlConnection does not specify an initial catalog or AttachDBFileName

但据我所知,我在 app.config 中正确指定了连接字符串:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="MyContext" connectionString="Data Source=db.sqlite" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

我尝试在上下文构造函数中自行设置数据库(使用SQLiteConnectionSQLiteCommand),但即使它成功创建了数据库和表,EF 仍然给我同样的错误。当我注释掉构造函数时它不会创建文件。

有没有办法实现我想要的?

【问题讨论】:

  • 我对 SQLite 了解不多。但是您是否可能需要提供 db 文件的确切路径?获取信息来自:damienbod.com/2015/08/30/…connectionstrings.com/sqlite
  • 配置文件中的所有内容都适用于 SQL Server。您是否通过 NuGet 安装了 Sqlite 连接器?
  • @GertArnold 是的,我尝试了 Microsoft.Data.SQLiteSystem.Data.SQLite

标签: c# asp.net entity-framework sqlite


【解决方案1】:

您应该使用System.Data.SQLite 代替System.Data.SqlClient。提供程序将使用 Microsoft 托管的 SQLite 包装器项目 Microsoft.Data.SQLite 而不是 System.Data.SQLite 项目。

<connectionStrings>
<add name="MyContext"
      connectionString="Data Source=|DataDirectory|db.sqlite"
      providerName="System.Data.SQLite" />
</connectionStrings>

将此构造函数添加到您的 DB 上下文类,因为有时默认构造函数无法正常工作。

public MyDBContext()
: base("MyContext")
{

}

希望它对你有用。

【讨论】:

  • 在连接字符串中,|DataDirectory| 是某种变量吗?我该如何设置?现在我只是指定Data Source=db.sqlite,因为老实说现在我并不关心文件的存储位置。
  • 我添加了构造函数,并在连接字符串中指定了“初始目录”。我正在使用 NuGet 的“System.Data.SQLite”包。我收到以下错误:“此操作需要连接到“主”数据库。无法创建到“主”数据库的连接,因为原始数据库连接已打开并且凭据已从连接字符串中删除。提供一个未打开的连接'
  • 只要把你必须存储数据库的目录放到调试目录中就可以了。
  • 您为什么使用 EF 6?您可以非常轻松地使用 EF Core 做同样的事情。
  • 我可以吗?但是 NuGet 包 System.Data.SQLite 会自动安装 EF6。我也应该安装 EF Core 吗?我无法卸载 EF6。现在也似乎认为我正在使用 SQL Server.. 现在的错误是它无法连接到 SQL Server..
【解决方案2】:

您的连接字符串不完整。

<add name="MyContext" connectionString="data source=db.sqlite;initial catalog=XXXXXXXXXXXX" providerName="System.Data.SQLite"/>

初始目录应包含您的数据库名称。数据源只是实例名称。

【讨论】:

  • 现在我收到此错误:This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection 如何获取 master 数据库的凭据?我从来没有设置它们
  • 将连接字符串更改为 connectionString="data source=db.sqlite;initial catalog=XXXXXXXXXXXX;integrated security=True;MultipleActiveResultSets=True"
  • 似乎想用SQL Server... EF 究竟是如何找出数据提供者类的?
  • 查看此链接。我认为这可能会有所帮助。 stackoverflow.com/questions/38557170/…您可能需要专注于使用的连接字符串。
  • 我现在正在使用 EF Core,它可以完美运行。感谢您的努力
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 2018-12-22
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 2017-09-27
  • 2018-08-24
相关资源
最近更新 更多