2

I m just using EF 5.0 and I've recreated a very simple DbContext that was working as is with EF 4.1.

Here context and model

public class AgenciesDatabaseContext : DbContext 
{
   public DbSet<Agency> Agencies { get; set; }
}

[Table("QryAgency")]
public class Agency
{
   [Key]
   public string CardCode { get; set; }
   public string DisplayName { get; set; }
   public string CardFName { get; set; }
   public string Address { get; set; }
   public string ZipCode { get; set; }
   public string City { get; set; }
}

I set in global.asax initializer for this context as null because the table already exists

Database.SetInitializer<ExtranetCentralizerContext>(null);

Here's the connection string in web.config :

<add name="AgenciesDatabase" providerName="System.Data.SqlClient" connectionString="..."/>

When I try to use the DbContext in the repository I get this error :

InnerException = {"Invalid column name '...'.\r\n Invalid column name '...'.\r\nInvalid column name '...'."}

It's strange because I could see that there is not connection made to my database.

What I don't understand is that I can make it work if I pass the connection string to the context like this :

 public class AgenciesDatabaseContext : DbContext 
 {
     public DbSet<Agency> Agencies { get; set; }

     public AgenciesDatabaseContext ()
            : base("AgenciesDatabase")
     {

     }
 }

There everything work fine. So my question is : isn't EF suposed to use the connection string that matches it's name (in this case AgenciesDatabase) ??? What makes it fail in this case ?

3
  • im confused, you say code first. Then your context's constructor can have ANY value you like. i would expect to some code in the constructor. Either hard coded or injected connection string. Commented Jan 30, 2013 at 11:00
  • My context's constructor here is only used to pass the name of the connection string. That makes it work but as I understand I should not need to pass it because my Context class has the same name as my Connection string. You follow me ? Commented Jan 30, 2013 at 11:22
  • @Arno2501 put your whole inner exception and connection string without modifying. Commented Jan 30, 2013 at 13:45

1 Answer 1

3

in your app.config the name should be AgenciesDatabaseContext, not only AgenciesDatabase.

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

2 Comments

You are right thank you ! I was so blind but this is absolutely true thanks a lot ! Still I don't understand why I was so convinced you just had to name it like your context without the "context" part pfff.
@Arno2501 I used to make the same mistake :). It is the inheritance of DbContext that enable the auto search of a connection string named as the class derived from DbContext :).

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.