无法通过PL中的DAL实体框架循环数据返回

时间:2012-02-12 10:08:10

标签: loops stored-procedures entity-framework-4 data-access-layer

我在我的项目中使用Entity Framework 4。一个基本要求是使用存储过程从数据库中获取数据并在表示层中循环这些数据,并与下拉列表,网格控件等绑定。

我遵循以下流程:

PL呼叫BLL和BLL呼叫DAL

示例代码:

DAL:

namespace DAL.Repository
{
    public class CountryRepository
    {
        public static IList GetCountry(string CountryId)
        { 
            using(MyDBEntities context=new MyDBEntities())
            {
               // it calls SP in DB thru EF to fetch data
                return context.GetCountry(CountryId).ToList();

            }
        }
    }
}

BLL

namespace BLL
{
   public class Country
    {
       public static IList GetCountry(string CountryId)
       {
           return DAL.Repository.CountryRepository.GetCountry(CountryId);
       }
    }
}

PL

  ddlCountry.DataTextField = "CountryName";
  ddlCountry.DataValueField = "CountryId";
  ddlCountry.DataSource= BLL.Country.GetCountry(string.Empty);
  ddlCountry.DataBind();

*此处绑定工作正常。但我不确定这是最好的选择。请建议我。

       System.Collections.IEnumerator lst= BLL.Country.GetCountry(string.Empty).GetEnumerator();
        while(lst.MoveNext())
        {
            string s = ((DAL.Entity.ORM.Country)(lst.Current)).Countryname;

         /* This is the main issue. To get data from [current]
         reference of [DAL.Entity.ORM.Country] is required.
         It is strongly not good practice to keep reference
         of DAL in PL. */
        }

使用带有存储过程的EF并在PL中独立使用这些数据从数据库获取数据的最佳方法是什么?

我使用静态方法是否安全?

请建议我。

1 个答案:

答案 0 :(得分:0)

  1. 理想情况下,您不希望在PL中引用您的DAL。您可以与PL和BL共享类型。 BL会在返回之前将您的DAL类型转换为此类型。

  2. 对于您显示的示例,静态方法应该没问题。