SQLite和EntityFramework的InvalidOperationException

3

我使用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.

此外,当我使用从NuGet下载的SQL Server CE运行此代码时,一切正常。
完整源代码:
Program.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>

谢谢回复。
1个回答

0

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

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

在编程中,DbConnection是系统定义的一个类,然后可以像这样初始化上下文:

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

基本上要区分您是想要连接 SQLite 还是不是 SQL Server。

希望这可以帮到您。


能不能在连接字符串中以某种方式实现这个? - ProfK
3
_isSqlServer成员从何处来?检查它的内容是什么,它在哪里进行检查? - ProfK
@ProfK _isSqlServer 是一个布尔变量,我建议使用一个标志来区分不同的连接。它的值可以通过配置文件中的标志或根据用户输入的情况进行分配。 - Ankit
你的不完整代码用英文写更易读,但还是谢谢。它有一定的意义。 - ProfK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接