2016-04-22 82 views
2

我想設置我的數據庫在我與數據庫新的項目,但我不斷收到這個問題:設置連接

An unhandled exception of type 'System.StackOverflowException' occurred in System.Core.dll 

我使用Web API文件夾中創建一個空的4.5項目。然後我添加了3個類庫BO(業務ojbect),BLL(業務邏輯)和DAL(數據訪問層)。

首先主要應用有web.config中像這樣

<connectionStrings> 
    <add name="ConnString" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Quantico;Integrated Security=True" providerName=".NET Framework Data Provider for SQL Server"/> 
    </connectionStrings> 

在我的BO庫連接字符串我有2級基地,性別

基地

public abstract class Base 
{ 
    public int ID { get; set; } 
    public Boolean isValid { get; set; } 
    public DateTime createdOn { get; set; } 
    public int createdID { get; set; } 
    /*public Person createdPerson { get; set; }*/ 
    public DateTime updatedOn { get; set; } 
    public int updatedID { get; set; } 
    /*public Person updatedPerson { get; set; }*/ 
} 

性別

public class Gender : Base 
{ 
    public string Description { get; set; } 
} 

在我DAL我有2類GenderDAL和Appcontext

AppContext is like this 
    public class AppContext : DbContext 
    { 
     public AppContext() : base("ConnString") 
     { 
     } 

     public DbSet<Gender> Gender { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 

      modelBuilder.Entity<Gender>().ToTable("Gender"); 
      modelBuilder.Entity<Gender>().HasKey(x => x.ID); 
      modelBuilder.Entity<Gender>().Property(x => x.Description).HasMaxLength(255); 
      modelBuilder.Entity<Gender>().Property(x => x.Description).IsRequired(); 
      modelBuilder.Entity<Gender>().Property(x => x.createdID).IsRequired(); 
      modelBuilder.Entity<Gender>().Property(x => x.createdOn).IsRequired(); 
     } 
    } 

GenderDAL是遵循

public class GenderDAL : AppContext 
    { 
     private AppContext db = null; 

     public GenderDAL() 
     { 
      db = new GenderDAL(); 
     } 

     public IEnumerable<Gender> GetGenders() 
     { 
      return db.Gender.ToList<Gender>(); 
     } 
    } 

問題發生在基地(CONNSTRING)和我的控制器是這樣

public class GenderController : ApiController 
{ 
    private GenderBLL db = null; 

    [HttpGet] 
    public IEnumerable<Gender> Genders() 
    { 
     db = new GenderBLL(); 
     return db.GetGenders(); 
    } 
} 

而我的商業邏輯在這個階段並不多,但它是這樣的

public class GenderBLL 
{ 
    private GenderDAL db = null; 

    #region constructor 
    public GenderBLL() 
    { 
     db = new GenderDAL(); 
    } 
    #endregion 

    public IEnumerable<Gender> GetGenders() 
    { 
     return db.GetGenders(); 
    } 
} 

如果我看看我的服務器資源管理器,我可以在dataconnection下看到我已連接到數據庫,但是沒有表存在。我相信這是我的問題,我不知道如何可以填充它,還是我錯過了哪一步

回答

4

您的問題無關,與連接字符串。看着你GenderDAL類:

public class GenderDAL : AppContext 
{ 
    private AppContext db = null; 

    public GenderDAL() 
    { 
     db = new GenderDAL(); 
    } 
    //.. 

GenderDAL類從AppContext繼承和構造函數裏面,你要創建一個new GenderDAL()(其中遞歸創建另一個嵌套GenderDAL,依此類推,無限)。這會導致無限遞歸,直到堆棧滿並且框架拋出StackOverflowException

我沒有看到從AppContext繼承DAL對象的任何正當理由,我強烈建議您刪除此類繼承。

+0

Gaaaaaaaaa。我沒有看到那個!我同意,刪除繼承。 – granadaCoder

+0

這給我一個新的問題,所以我所做的是刪除,:Appcontext,現在我得到這 EntityFramework.dll中發生類型'System.ArgumentException'的異常,但未在用戶代碼中處理 其他信息:具有不變名稱'SQL Server的.NET Framework數據提供程序'的ADO.NET提供程序未在計算機或應用程序配置文件中註冊,或者無法加載。詳情請參閱內部例外。 – Jseb

+0

如果您使用本地數據庫版本13,請檢查此鏈接http://www.michael-whelan.net/sql-server-localdb-2014-connection-string/ –

1

嘗試

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 


     modelBuilder.Entity<Gender>().ToTable("Gender"); 
     modelBuilder.Entity<Gender>().HasKey(x => x.ID); 
     modelBuilder.Entity<Gender>().Property(x => x.Description).HasMaxLength(255); 
     modelBuilder.Entity<Gender>().Property(x => x.Description).IsRequired(); 
     modelBuilder.Entity<Gender>().Property(x => x.createdID).IsRequired(); 
     modelBuilder.Entity<Gender>().Property(x => x.createdOn).IsRequired(); 

    /* optional addition */ 
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

     base.OnModelCreating(modelBuilder); 

    } 

PS

你可以得到一個「種子數據」的例子從這裏:

https://github.com/ProgrammingAspNetMvcBook/CodeExamples/tree/master/Ebuy.Common/DataAccess

另外,請檢查您的連接字符串和配置。

例(基於羅斯文)

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <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=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="NorthwindContext" connectionString="Data Source=MyServer\Instance;Initial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True;Application Name='Northwindy'" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    <providers> 
     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 
    </providers> 
    </entityFramework> 
</configuration> 
+0

不幸當我去http:// localhost:53069/api/gender我仍然收到system.core.dll中發生的類型爲system.stack.overflowexception的未處理的異常。這裏的問題發生在公共AppContext():base(「ConnString」) – Jseb