3

I'm working with Visual Studio 2013 and trying to create sample project using SQLite and EntityFramework downloaded from NuGet.

When i run program i got this exception on the line below and i don't have any idea to repair this.

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

Exception:

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.

Additionaly, when i run this code with SQL Server CE (dowloaded also from NuGet) everything works OK.

Full Source code:

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>

Thanks for reply.

Symeg
  • 115
  • 1
  • 10

1 Answers1

0

you need to edit your context constructor like this :

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

where DbConnection is system defined class , and then initialize context like :

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

Basically to distinguish whether you want to establish connection to SQLite and not sqlserver

Hope this helps.

Ankit
  • 5,733
  • 2
  • 22
  • 23
  • Can't one do that in the connection string somehow? – ProfK Sep 15 '14 at 16:29
  • 3
    Where the blazes does the `_isSqlServer` member come from, and what and where checks it? – ProfK Sep 15 '14 at 17:15
  • @ProfK _isSqlServer is a boolean variable , i am trying to suggest to use a flag to differentiate between difeerent connections. It's value can be assigned via a flag from config file or depending upon circumstances from user input – Ankit Jul 17 '17 at 10:47
  • Your incomplete code is much more readable in English, but thanks. It makes fair sense. – ProfK Jul 17 '17 at 15:28