【问题标题】:SQLite and EntityFramework InvalidOperationExceptionSQLite 和 EntityFramework InvalidOperationException
【发布时间】:2014-09-01 02:48:31
【问题描述】:

我正在使用 Visual Studio 2013 并尝试使用从 NuGet 下载的 SQLite 和 EntityFramework 创建示例项目。

当我运行程序时,我在下面的行中遇到了这个异常,我不知道要修复它。

context.Persons.Add(new Person() { Name = "aaa111", Surname = "bbb111" });

例外:

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

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

另外,当我使用 SQL Server CE(也从 NuGet 下载)运行此代码时,一切正常。

完整源代码:

程序.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace sqlcetut
{
    public class Person
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }
    }

    public class Ctx : DbContext
    {
        public Ctx()
            : base(@"Data Source=sample.sqlite")
        {
            Database.SetInitializer(new Initial());
        }

        public DbSet<Person> Persons { get; set; }

        public class Initial : DropCreateDatabaseIfModelChanges<Ctx>
        {
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("start");
            using (Ctx context = new Ctx())
            {
                context.Persons.Add(new Person() { Name = "aaa111", Surname = "bbb111" });
                context.Persons.Add(new Person() { Name = "aaa222", Surname = "bbb222" });
                context.Persons.Add(new Person() { Name = "aaa333", Surname = "bbb333" });

                context.SaveChanges();
            }

            Console.WriteLine("end");
            Console.ReadLine();
        }
    }
}

App.config:

<?xml version="1.0" encoding="utf-8"?>
<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.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite" />
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".Net Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
    </DbProviderFactories>
  </system.data>
</configuration>

感谢回复。

【问题讨论】:

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


    【解决方案1】:

    您需要像这样编辑上下文构造函数:

    public CaseDataDataContext(DbConnection connection)
                : base(connection, true) 
            {
                _isSqlServer = false; 
            }
    

    其中 DbConnection 是系统定义的类,然后像这样初始化上下文:

    CaseDataDataContext projectDBContext = new CaseDataDataContext(new SQLiteConnection() { ConnectionString = "Data Source =./CaseDataDB.s3db" });
    

    基本区分是要建立SQLite连接还是不要sqlserver

    希望这会有所帮助。

    【讨论】:

    • 不能以某种方式在连接字符串中做到这一点吗?
    • _isSqlServer 成员的火焰来自哪里,检查的内容和位置是什么?
    • @ProfK _isSqlServer 是一个布尔变量,我试图建议使用一个标志来区分不同的连接。它的值可以通过配置文件中的标志或根据用户输入的情况来分配
    • 您的不完整代码在英文中的可读性更高,但谢谢。这是有道理的。
    猜你喜欢
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多