在n层c#层中设置连接

时间:2016-04-22 17:05:29

标签: c# asp.net asp.net-mvc-4 asp.net-web-api asp.net-web-api2

我正在尝试使用数据库在我的新项目中设置我的数据库,但是我不断遇到这个问题:

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>();
        }
    }

问题发生在base(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下看到我已连接到数据库,但是不存在任何表。我相信这是我的问题,我不知道如何填充它,或者我错过了什么步骤

2 个答案:

答案 0 :(得分:4)

您的问题与连接字符串无关。查看您的GenderDAL课程:

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

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

您的GenderDAL类继承自AppContext,并且在构造函数中创建了一个new GenderDAL()(以递归方式创建另一个嵌套GenderDAL,依此类推,无限制)。这会导致无限递归,直到堆栈变满并且框架抛出StackOverflowException

我没有看到从AppContext继承您的DAL对象的任何正当理由,我强烈建议您删除此类继承。

答案 1 :(得分: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

您可以获得种子数据&#34;例子来自:

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

同时检查连接字符串和配置。

示例(基于Northwind)

<?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>