2

I am using VS 2013. I am trying to use SQL Server Compact with Entity Framework 6. Created database and .EDMX model in VS 2012 and opened in VS 2013. I installed latest EF and EF SQL.CE from NuGet. When I try to run I am getting the following error

No connection string named 'SomeDbFile' could be found in the application config file.

app.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" 
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
            <parameters>
                <parameter value="System.Data.SqlServerCe.4.0" />
            </parameters>
        </defaultConnectionFactory>
        <providers>
            <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
            <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
    </entityFramework>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SqlServerCe.4.0" />
            <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
    </system.data>
    <connectionStrings>
        <add name="SomeDbFile" 
             connectionString="metadata=res://*/EDataModel.csdl|res://*/EDataModel.ssdl|res://*/EDataModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;data source=|DataDirectory|\dbfile.sdf&quot;" 
             providerName="System.Data.SqlServerCe.4.0" />
    </connectionStrings>
</configuration>

Generated DbContext:

public partial class Entities : DbContext
{
    public Entities() : base("name=SomeDbFile")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<category> category { get; set; }
    public DbSet<item> item { get; set; }
    public DbSet<order> order { get; set; }
    public DbSet<order_line> order_line { get; set; }
}
4
  • Do you have multiple projects? What kind of platform is this running on? WPF, web, something else? Commented May 5, 2015 at 17:13
  • @MattiVirkkunen sorry forgot to mention I am creating a winforms application. Though I have 2 separate project. One is class library, another is windows form app. Commented May 5, 2015 at 17:15
  • 1
    Did you put your connection settings in the winforms app app.config too? Commented May 5, 2015 at 17:16
  • @MattiVirkkunen but I am using EF only in class lib. Commented May 5, 2015 at 17:18

2 Answers 2

2

You probably forgot to put the connection settings in your main application (the thing that is actually run) app.config.

app.config files in class library projects do nothing at runtime and are generally useless. (Some IDE features might need them though)

Sign up to request clarification or add additional context in comments.

5 Comments

Beat me to it @Matti, this is more than likely the solution.
Now I am getting Keyword not supported: 'metadata'. error.
@zazu: Looks like it's finding the connection string fine then. Please create another question about your new, unrelated error.
@MattiVirkkunen inside the connection string it starts with metadata. I copied the whole config from dll project, but it didnt help. Please help me, I have lost half of the day trying to fix this problem.
@zazu: Create a new question that explains your problem. You get to put in a more descriptive title and you'll get more visibility too.
1

When a context gets generated it normally has the connection string set to the same name as the context so it would be worth checking that there isn't a problem there. That way as long as the connection string name is ok you should be able to just use the original EF connection string in the client web.config.

Or you can comment out the "throw new UnintentionalCodeFirstException();" line from OnModelCreating to use the normal connection string.

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //comment out this line to allow for a normal connection string 
        throw new UnintentionalCodeFirstException(); 
    }

Please check these links for EF connection strings

 https://msdn.microsoft.com/en-us/library/cc716756(v=vs.110).aspx  
https://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.providerconnectionstring(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder.providerconnectionstring(v=vs.110).aspx

Try to change the provider name in connection string to providerName="System.Data.EntityClient"

6 Comments

Didnt help, I am getting the error when I create a new instance of dbcontext
You need to have connection string in the configuration file of the startup project. Just find out your start up project and copy the connection string to the web.config file of that start up project. After doing that you should not get the above error.
Now I am getting Keyword not supported: 'metadata'. error.
The string you passed is not a valid database connection string, it's an EF connection string that contains a SQL Server connection string in its provider connection string parameter. WebSecurity.InitializeDatabaseConnection expects a valid database connection string To avoid parsing the connection string yourself, you can use the EntityConnectionStringBuilder class to parse the string and retrieve the database connection string from its ProviderConnectionString property Just refer the links in my updated answer
Did solve this issue?. If my answer is useful please upvote or accept my answer if not useful you can downvote.
|

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.