Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am getting error like this.
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.

here is my code
C#
namespace EFfistSample
{
    class Program
    {
        static void Main(string[] args)
        {
            var person = new Person { FirstName = "Sreekanth", LastName = "Vankamamidi", BirthDate = DateTime.Now };
            using (var context = new MyContext())
            {
                context.Persons.Add(person);
                context.SaveChanges();
            }
            Console.Write("Person Saved");
            Console.ReadLine();
        }
    }
}


person.cs
C#
namespace EFfistSample
{
  public class Person
    {
      public int PersonID { get; set; }
      public string LastName { get; set; }
      public string FirstName { get; set; }
      public DateTime BirthDate { get; set; }

  }
}


mycontext.cs
XML
namespace EFfistSample
{
   public class MyContext : DbContext
    {
       public MyContext()
       { }

       public DbSet<Person> Persons { get; set; }
    }
}


app.config i have given like this.
XML
<configuration>
  <appSettings>

  </appSettings>
  <connectionStrings>
    <add name="MyContext" connectionString="Data Source=(local); Initial Catalog=mydb; Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>

</configuration>


please help me

thank you.
Posted
Comments
[no name] 14-Sep-12 12:28pm    
And the exception that was thrown was....?
ShotDriller 14-Sep-12 12:30pm    
post your exception details
sreekanthv12 14-Sep-12 12:49pm    
exception .
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.

Try placing
XML
<connectionStrings> ... </connectionStrings></
section after
XML
<configsections> ...</configsections>
in your web.config file
 
Share this answer
 
v2
Comments
sreekanthv12 14-Sep-12 12:58pm    
i have given but it is giving same error.
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
Ibrahim Islam 22-Sep-13 2:53am    
Thanks! That solved the problem.
The EF connection string is different from normal connection string. See: http://msdn.microsoft.com/en-us/library/bb896291.aspx[^]
 
Share this answer
 
Comments
sreekanthv12 14-Sep-12 13:06pm    
For Code First we have to give normal connection string.
Zoltán Zörgő 14-Sep-12 13:10pm    
Hmmm... you are right.
Have you seen this thread: http://stackoverflow.com/questions/11775513/system-data-entity-internal-configfile-entityframeworksection ?
It looks there is a package error with newest EF.
I think you must have missed to mention the personId while initializing a new Person.
Try Adding it. And if it doesn't help please provide more details of error.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900