GWP
GWP

Reputation: 451

Where is my connection string?

I have created a project call Repository.EF to handle Data access in a n-tire solution. I have added EF to the Repository.EF project where I have all my POCO's . Then I created a DbContext class in that project like this.

namespace LearningSpike.Repositories.EF
{
class GlassContractDbContext:DbContext
    {
    public GlassContractDbContext() : base("GlassContractContext")
    {
    }

    public DbSet<MetalStock> MetalStock { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Configurations.Add(new MetalStockConfiguration());
    } 
}

Then went to package manager console and did

Enable-Migrations 

Then set the

 AutomaticMigrationsEnabled = true;

Then

Update-Database

Everything is working fine. But the problem is, I don't know where the connection string is. It seems like there is no connectionString in that particular project. I know if I had an MVC4/5 template, there will be a connectionString in the web.config. How can I find the connection string? How do I configure things now? For example I remember doing this with the connectionString in a MVC5 app

 MultipleActiveResultSets=true

How do I do it now?

Thanks! Cheers!

PS

Also I have the following code in my App.config in the Repository.EF Project

 <?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" />
 <!-- For more information on Entity Framework configuration, visit 
 http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
 <entityFramework>
 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory,
 EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" 
    type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
  </providers>

</entityFramework>

Upvotes: 1

Views: 1212

Answers (1)

Matt
Matt

Reputation: 81

When running the package manager console it will default to use the start-up project (unless you specify the -project param in the command or use the drop down in console manager). From there it will look for your connection strings in the config files of that project. If that is a web project this will be in the web.config.

If you have not added your own connection string to that project, EF will be able to use its own one derived from the project name and create an mdf file which it attaches on the fly during runtime.

If you want to add a connection string (you can do this is any of your config files - however by the sounds of it you want to add this to the app.config in your datalayer project) you can add this below your config sections:

<connectionStrings>
<add name="MyDatabase"
     providerName="System.Data.SqlClient"
     connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"/>
</connectionStrings>

Upvotes: 1

Related Questions