5

I need to connect to a database in Sqlite so i downloaded and installed System.Data.SQLite and with the designer dragged all my tables.

The designer created a .cs file with

public class Entities : ObjectContext

and 3 constructors:

1st

public Entities() : base("name=Entities", "Entities")

this one load the connection string from App.config and works fine.

App.config

<connectionStrings>
    <add name="Entities" connectionString="metadata=res://*/Db.TracModel.csdl|res://*/Db.TracModel.ssdl|res://*/Db.TracModel.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=C:\Users\Filipe\Desktop\trac.db&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

2nd

public Entities(string connectionString) : base(connectionString, "Entities")

3rd

public Entities(EntityConnection connection) : base(connection, "Entities")

Here is the problem, i already tried n configuration, already used EntityConnectionStringBuilder to make the connection string with no luck.

Can you please point me in the right direction!?

EDIT(1)

I can do my queries if i use de parameterless constructor but i need to change the connection string, i can't use the one in my app.config.

How can i construct a valid connection string?!

0

1 Answer 1

7

Found it =)

if you use the EntityConnectionStringBuilder to specify the Metadataand the Provider and use the SqlConnectionStringBuilderto build the provider connection string and set the DataSource to your DB. You can connect =)

var con = new EntityConnectionStringBuilder()
  {
    Metadata = @"res://*/Db.TracModel.csdl|res://*/Db.TracModel.ssdl|res://*/Db.TracModel.msl",
    Provider = @"System.Data.SQLite",
    ProviderConnectionString = new SqlConnectionStringBuilder()
      {
        DataSource = db,
      }.ConnectionString,
  };

connection = con.ConnectionString;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.