【问题标题】:How to refactor the connectionString in my Entity Framework & ASP.NET MVC project?如何在我的实体框架和 ASP.NET MVC 项目中重构 connectionString?
【发布时间】:2021-09-08 09:41:45
【问题描述】:

我有大量的存储过程要使用,我必须使用实体框架。

例如,我得到了这个控制器,我只是在其中调用数据库来显示我的表:

public class CarguioController : Controller
{
    public ActionResult Index(int? page)
    {
        string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

        using (SqlConnection conn = new SqlConnection(cs))
        {
            // establece conneción
            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@MODO";
            param1.SqlDbType = SqlDbType.Int;
            param1.Value = 2;

            SqlCommand cmdProcedure = new SqlCommand(@"Almacen.[PRC_Carguio]", conn);
            cmdProcedure.Parameters.Add(param1);

            conn.Open();

            cmdProcedure.CommandType = CommandType.StoredProcedure;

            SqlDataReader dr = cmdProcedure.ExecuteReader();
            List<CarguioViewModel> lst = new List<CarguioViewModel>();

            int pageNumber = page ?? 1;
            int pageSize = 8;

            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    lst.Add(new CarguioViewModel
                    {
                        Carguio_ID = dr.GetInt32(0),
                        Vehiculos_ID = dr.GetInt32(1),
                        ManifiestoCarga_ID = dr.GetInt32(2),
                        Guia_ID = dr.GetInt32(3),
                        Programaciones_ID = dr.GetInt32(4),
                        Numero = dr.GetInt32(5),
                        NroMobil = dr.GetString(6),
                        Fecha = dr.GetDateTime(7),
                        Usuarios_ID = dr.GetInt32(8),
                        Sucursales_IS = dr.GetInt32(9)
                    });
                    //display retrieved record
                }

                return View(lst.ToPagedList(pageNumber, pageSize));
            }
            else
            {
                Console.WriteLine("No data found.");
            }

            dr.Close();
            conn.Close();
        }

        return View();
    }
}

如您所见,我必须多次连接 SQL Server 数据库。也许您已经在 ASP.NET MVC 项目中做过类似的工作,或者有任何想法重构我的代码?

我有 30 多张桌子,每个人都有更多的 Crud 和其他功能。

我一直在寻找这个,但有同样的例子。

string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

【问题讨论】:

    标签: asp.net-mvc entity-framework-4 refactoring connection-string


    【解决方案1】:

    我添加了一个名为 ADO.Net 实体数据模型的新元素,我可以在其中检索所有存储过程,这很有帮助

    I added a new element called ADO.Net Entity Data Model

    嗯,现在我的代码比以前短了:

    public ActionResult Index(int? page)
        {
            List<CarguioModel> lst = new List<CarguioModel>();
    
            int pageNumber = page ?? 1;
            int pageSize = 8;
    
            using (MarviBKPEntities prcAlm = new MarviBKPEntities())
            { 
                List<PRC_Carguio_Result> prc = prcAlm.PRC_Carguio(2, null, null, null, null, null, null, null, null, null, null).ToList();
                return View(prc.ToPagedList(pageNumber, pageSize));
            }
    
                
            return View();
        }
    

    你觉得呢?会不会导致性能不佳?

    【讨论】:

      【解决方案2】:

      您可以在通用实用程序类中创建来读取连接,以便它留在代码中的一个位置,并在您需要的任何地方从通用实用程序类中读取连接值。

      void Main()
      {
          string cn = GeneralUtility.getConnectionString();
      }
      
      public class GeneralUtility
      {
          public static string getConnectionString()
          {
              string cs = "";
              try
              {
                  cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
              }
              catch (Exception ex)
              {   
                  throw new Exception("Connection String Error " + ex.Message.ToString());
              }
              return cs;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-02-20
        • 1970-01-01
        • 2012-05-17
        • 2013-10-25
        • 2012-10-18
        • 2023-03-22
        • 2010-09-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多