0

I am trying to connect to a database without using App.Config but i keep getting the following error:

An unhandled exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.dll

Additional information: The underlying provider failed on ConnectionString.

I can't see where I've gone wrong so i thought i'd ask here.

namespace MyNameSpace
    {
        using System;
        using System.Data.Entity;
        using System.Data.Entity.Core.EntityClient;
        using System.Data.Entity.Infrastructure;

        public partial class Entities : DbContext
        {
            public Entities()
                : base(entityString.ToString())
            {
            }

            public static EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
            {
                Provider = "System.Data.SqlServerCe.4.0",          
                Metadata = "res://*/RS.csdl|res://*/RS.ssdl|res://*/RS.msl",
                ProviderConnectionString = @"C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"
            };  



        }
    }

Thank you in advance for your help.

3 Answers3

0

Try this : make this parameterise

    public Entities(string connString)
        : base(connString)
    {
    }

and pass string connection string when creating object of Context class.

public class TestController : Controller
{
 Entity _context = new Entity("data source=Dev-4;initial catalog=test1;
 integrated security=True;MultipleActiveResultSets=True;  
 App=EntityFramework");
}
  • That won't work because then i have to pass the connection string every time i create a new instance. –  Dec 11 '15 at 05:39
  • try this : public Model1() : base(connString) { } public static string connString = "data source=tesst;initial catalog=test1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"; – Vaibhav Chaurasiya Dec 11 '15 at 05:43
0

Try this : here you din't need to pass connection string again and again -->

    public Model1()
        : base(connString)
    {
    }

    public static string connString = "data source=tesst;initial catalog=test1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";

Use This method when using Database First Model of Entity Framework :

    public test1Entities()
         : base(nameOrConnectionString: ConnectionString())
    {
    }

    private static string ConnectionString()
    {
        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        sqlBuilder.DataSource = "DEV-4";
        sqlBuilder.InitialCatalog = "test1";
        sqlBuilder.PersistSecurityInfo = true;
        sqlBuilder.IntegratedSecurity = true;
        sqlBuilder.MultipleActiveResultSets = true;

        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
        entityBuilder.Metadata = "res://*/";
        entityBuilder.Provider = "System.Data.SqlClient";

        return entityBuilder.ToString();
    }
  • You have to use EntityConnectionStringBuilder when using a Database first model, This was the first thing i tried, It didn't work. –  Dec 11 '15 at 05:43
  • Hi, please check another method written by me, this will working when using Database First Model. – Vaibhav Chaurasiya Dec 11 '15 at 05:53
  • Please note : Provider = "System.Data.SqlServerCe.4.0" in my original post. Also, This doesn't account for the password. –  Dec 11 '15 at 05:56
  • 1
    please try this : public Entities() : base(new SqlCeConnection("--string connection string--"), contextOwnsConnection: true) { } – Vaibhav Chaurasiya Dec 11 '15 at 06:06
  • I reckon that would probably work but i prefer the answer from Jcl. –  Dec 11 '15 at 06:19
0

The problem is that you are passing your sdf file directly on your connection string. Try changing:

ProviderConnectionString = @"C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"

To:

ProviderConnectionString = @"Data Source=C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"

Or better yet, use a SqlCeConnectionStringBuilder to construct this property:

var connectionStringBuilder = new SqlCeConnectionStringBuilder();
connectionStringBuilder.DataSource = @"C:\RestOfPath\database.sdf";
connectionStringBuilder.Password = "3476dfg423434563466e85rcsd";
EFConnectionBuilder.ProviderConnectionString = connectionStringBuilder.ToString(),
Jcl
  • 27,696
  • 5
  • 61
  • 92
  • Thank you! I had a feeling it was something simple! –  Dec 11 '15 at 06:06
  • What got me was that in your question you specified: `Additional information: The underlying provider failed on ConnectionString.`. Are you sure it didn't say `Additional information: The underlying provider failed on Open`? The first message sounds weird and I've never seen it :-) – Jcl Dec 11 '15 at 06:07
  • Ty for the updated answer, That will make the next step easier! Cheers! –  Dec 11 '15 at 06:09
  • It most certainly said "ConnectionString" i copy and pasted it, I didn't find much googling it. –  Dec 11 '15 at 06:10
  • 1
    That is weird! Then again, I haven't mangled a lot with Ce :-) Glad it helped – Jcl Dec 11 '15 at 06:11