Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET4.0
i am getting error like this.
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
 
here is my code
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
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
namespace EFfistSample
{
   public class MyContext : DbContext
    {
       public MyContext()
       { }
 
       public DbSet<Person> Persons { get; set; }
    }
}
 
app.config i have given like this.
<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 14-Sep-12 6:17am
Comments
Wes Aday at 14-Sep-12 12:28pm
   
And the exception that was thrown was....?
ShotDriller at 14-Sep-12 12:30pm
   
post your exception details
sreekanthv12 at 14-Sep-12 12:49pm
   
exception .
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try placing
<connectionStrings> ... </connectionStrings></
section after
<configsections> ...</configsections>
in your web.config file
  Permalink  
v2
Comments
sreekanthv12 at 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 at 22-Sep-13 2:53am
   
Thanks! That solved the problem.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The EF connection string is different from normal connection string. See: http://msdn.microsoft.com/en-us/library/bb896291.aspx[^]
  Permalink  
Comments
sreekanthv12 at 14-Sep-12 13:06pm
   
For Code First we have to give normal connection string.
Zoltán Zörgő at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 14 Oct 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100