使用Entity Framework Code First将Visual Studio 2013项目转换为2015时出现System.StackOverflowException错误

Dil*_*165 10 entity-framework ef-code-first visual-studio-2013 visual-studio-2015

我们创建了"Visual Studio 2012 Ultimate中的MVC项目",并使用了"Code First".现在我们要将此项目转换为"Visual Studio 2015 Professional".

目前的项目配置: -

.Net Framework版本:4.5

MVC版本:5.0(5.2.2)

使用的实体框架:6.0(6.1.1)

问题:我们在"OnModelCreating"函数中遇到了错误.下面我写了代码片段.

protected virtual void OnModelCreating(DbModelBuilder modelBuilder) <---       Error throw at this line System.StackOverflowException
{
   //Foreign key relation

}
Run Code Online (Sandbox Code Playgroud)

StackOVerFlow异常错误可能是由于递归

我们面临的不同的不同问题

1.有时我在OnModelCreating方法中评论外键关系比它的工作正常.2.这个错误的可能原因可能是递归,但它在VS 2012和VS 2013中运行良好.现在这个Visual Studio 2015版本出了什么问题.
3.在Visual Studio 2015中,OnModelCreating方法的某些部分在一台机器上正常工作,但相同的代码在其他机器中不起作用.

Dil*_*165 0

最后,我通过为 Fluent API 创建单独的类(因为它是内存异常问题)并通过将 Fluent API 文件划分为多个文件(意外的解决方案,但工作正常)解决了 Sytem.StackOverflowException 错误,我已经解决了它。可能是由于 Fluent API 中的大量代码行(我之前有超过 10000 行代码)而发生此问题,因此对于具有不同配置和基于 CPU 使用情况的不同机器(计算机),它的行为是不同的。

例如

如果您有早期的模型创建文件,如下所示

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{        
    modelBuilder.HasMany(e => e.LoginC)
    .WithOptional(e => e.LoginLoginC)
    .HasForeignKey(e => new { e.Cmp_Id, e.Login_C_Id });       


     modelBuilder.HasMany(e => e.LoginU)
    .WithOptional(e => e.LoginLoginU)
    .HasForeignKey(e => new { e.Cmp_Id, e.Login_U_Id});   


     modelBuilder.HasMany(e => e.LoginD)
     .WithOptional(e => e.LoginLoginD)
     .HasForeignKey(e => new { e.Cmp_Id, e.Login_D_Id });

}
Run Code Online (Sandbox Code Playgroud)

现在单独的 LoginEntity 可以解决这个问题,如下所示

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{                 

     modelBuilder.Configurations.Add(new LoginEntityTypeConfiguration());

}
Run Code Online (Sandbox Code Playgroud)

现在创建名为 LoginEntityTypeConfiguration.cs 的单独类文件

public class LoginEntityTypeConfiguration : EntityTypeConfiguration<Login>
{
    public LoginEntityTypeConfiguration()
    {
        this.HasMany(e => e.LoginC)
        .WithOptional(e => e.LoginLoginC)
        .HasForeignKey(e => new { e.Cmp_Id, e.Login_C_Id });            

        this.HasMany(e => e.LoginU)
        .WithOptional(e => e.LoginLoginU)
        .HasForeignKey(e => new { e.Cmp_Id, e.Login_U_Id });

        this.HasMany(e => e.LoginD)
        .WithOptional(e => e.LoginLoginD)
        .HasForeignKey(e => new { e.Cmp_Id, e.Login_D_Id });            

   }
}
Run Code Online (Sandbox Code Playgroud)

要在实体框架中为 Fluent API 创建单独的类,您可以参考http://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-code-first.aspx

现在很可能它可以解决您的问题,如果您的问题仍未解决,您可以将 LoginEntityTypeConfiguration.cs 文件划分为多个类,如下所示

// LoginEntityTypeConfiguration.cs
 public class LoginEntityTypeConfiguration : EntityTypeConfiguration<Login>
 {
    public LoginEntityTypeConfiguration()
    {
        this.HasMany(e => e.LoginC)
        .WithOptional(e => e.LoginLoginC)
        .HasForeignKey(e => new { e.Cmp_Id, e.Login_C_Id });            

       // Create instance of newly created separate file than call it's method
        LoginPart1EntityTypeConfiguration LoginPart1EntityTypeConfiguration = new LoginPart1EntityTypeConfiguration();
        LoginPart1EntityTypeConfiguration.LoginEntityTypeConfiguration(this);            

   }
}

 /// create LoginPart1EntityTypeConfiguration.cs file

public class LoginPart1EntityTypeConfiguration
{
    public void LoginEntityTypeConfiguration(LoginEntityTypeConfiguration LoginEntityTypeConfiguration)
    {

         LoginEntityTypeConfiguration.HasMany(e => e.LoginU)
        .WithOptional(e => e.LoginLoginU)
        .HasForeignKey(e => new { e.Cmp_Id, e.Login_U_Id });


        LoginEntityTypeConfiguration.HasMany(e => e.LoginD)
        .WithOptional(e => e.LoginLoginD)
        .HasForeignKey(e => new { e.Cmp_Id, e.Login_D_Id });

 }

}
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助您克服这个问题。