首先是实体框架代码中的错误。 [英] Error in Entity Framework code first.

查看:63
本文介绍了首先是实体框架代码中的错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到这样的错误。

'System.Data.Entity.Internal.AppConfig'的类型初始化程序引发异常。



这是我的代码

 命名空间 EFfistSample 
{
class 程序
{
静态 void Main( string [] args)
{
var person = new 人{FirstName = Sreekanth ,LastName = Vankamamidi,BirthDate = DateTime.Now};
使用 var context = new MyContext())
{
context.Persons.Add(person);
context.SaveChanges();
}
Console.Write( 保存人员);
Console.ReadLine();
}
}
}





person.cs

< pre lang =cs> 名称空间 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

< pre lang =xml> namespace EFfistSample
{
public class MyContext:DbContext
{
public MyContext()
{}

public DbSet < > 人员{get;组; }
}
}





app.config我这样给了。

 <   configuration   >  
< appSettings >

< / appSettings >
< connectionStrings >
< add < span class =code-attribute> name = MyContext connectionString = 数据源=(本地);初始目录= mydb; Integrated Security = True; providerName = System.Data.SqlClient / >
< / connectionStrings >
< configSections >
<! - 有关实体框架配置的更多信息,请访问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 >
< < span class =code-leadattribute> entityFramework
>
< defaultConnectionFactory type = System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework / >
< / entityFramework >

< / configuration >





请帮帮我



谢谢。

解决方案

尝试放置

 <   connectionStrings  >  ... <   / connectionStrings  >  <   /  



 <   configsections  >  ... <   / configsections  >  

.config file


EF连接字符串与普通连接字符串不同。请参阅: http://msdn.microsoft.com/en-us/library/bb896291.aspx [ ^ ]


我认为你在初始化一个新人时一定错过提及personId。

尝试添加它。如果没有帮助,请提供更多错误详情。


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.

解决方案

Try placing

<connectionStrings> ... </connectionStrings></

section after

<configsections> ...</configsections>

in your web.config file


The EF connection string is different from normal connection string. See: http://msdn.microsoft.com/en-us/library/bb896291.aspx[^]


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.


这篇关于首先是实体框架代码中的错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆