SQLite和EntityFramework InvalidOperationException

时间:2014-07-10 16:10:35

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

我正在使用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下载)运行此代码时,一切正常。

完整源代码:

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 :(得分:1)

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

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

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

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

基本上要区分是否要建立与SQLite的连接而不是sqlserver

希望这有帮助。