【问题标题】:Entity Framework and DBContext data retrieval实体框架和 DBContext 数据检索
【发布时间】:2016-01-26 16:04:29
【问题描述】:

我有一个 Web API,其中我的存储库类对数据进行了硬编码。我想修改它以从 SQL Server 数据库中获取相同的数据。我在 DAL 文件夹中创建了 DB Context 类,并在我的 web.config 文件中创建了一个与上下文类同名的连接字符串 - MyClassesContext

 public class myRepository
{

public myClasses.Type[] GetAllTypes()
{

    return new myClasses.Type[]
    {
        new myClasses.Type 
        {
            typeId="1",
            typeVal = "New"
        },
        new myClasses.Type 
        {
            typeId="2",
            typeVal = "Old"
        }
   };

}

public myClasses.Employee[] GetAllEmployees()
{

    return new myClasses.Employee[]
    {
        new myClasses.Employee 
        {
            empId="111111",
            empFName = "Jane",
            empLName="Doe"
        },
        new myClasses.Employee 
        {
            empId="222222",
            empFName = "John",
            empLName="Doe"
        }
   };

}

public bool VerifyEmployeeId(string id)
{

    myClasses.Employee[] emp = new myClasses.Employee[]
    {
        new myClasses.Employee 
        {
            empId="111111",
            empFName = "Jane",
            empLName="Doe"
        },
        new myClasses.Employee 
        {
            empId="222222",
            empFName = "John",
            empLName="Doe"
        }
   };

    for (var i = 0; i <= emp.Length - 1; i++)
    {
        if (emp[i].empId == id)
            return true;
    }
    return false;
  }
}

和我的模型类:

public class myClasses
{

    public class Employee
    {
        public string empId { get; set; }
        public string empFName { get; set; }
        public string empLName { get; set; }
    }

    public class Type
    {
        public string typeId { get; set; }
        public string typeVal { get; set; }
    }
}

这是我的 DBContext:

using System.Data.Entity;
using myClassesAPI.Models;

namespace myClassesAPI.DAL
{
    public class myClassesContext : DbContext
    {

        public DbSet<myClasses.Employee> Employees { get; set; }
        public DbSet<myClasses.Type> Types { get; set; }

    }
}

现在这里缺少的链接是如何连接 DBContext 类。我做了大量的谷歌搜索,但找不到任何相关的东西。想知道是否有人可以指出我正确的方向

【问题讨论】:

    标签: entity-framework model-view-controller asp.net-web-api


    【解决方案1】:

    考虑到一切都已配置并正常工作(连接字符串、数据库访问等),在您的存储库中,只需使用您的 DbContext 类即可。获取所有类型列表的示例:

    public List<Type> GetAllTypes()
    {
        //Create an instance of your dbContext Class.
        using (myClassesContext  context = new myClassesContext ())
        {
            //Return all the Type's
            return context.Type.ToList()
        }
    }
    

    我认为您需要阅读和研究更多有关 EntityFramework 的内容,以便更好地了解事物的工作原理,以及如何使用 LINQ 查询数据库。这是一个很好的起点:

    http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    编辑 我使用这个连接字符串的例子:

    <add name="myClassesContext "
        providerName="System.Data.SqlClient"
        connectionString="Data Source=ServerName\InstanceName;Initial Catalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
    

    然后,您在 DbContext 文件中设置相同的连接字符串名称:

    public myClassesContext ()
            : base(Connection.GetConnectionString("myClassesContext"))
    {
    }
    

    【讨论】:

    • 问题是,如您所见,在我的存储库中,所有内容都是硬编码的,我不使用数据库。
    • 我用本地 SQL Server 实例的连接字符串示例更新了我的答案。再看一遍。
    • 我收到错误“System.Data.Entity.DbSet 不包含 ToList 的定义”
    • 您阅读我发布的教程了吗?您似乎缺乏很多关于实体框架如何工作的基本知识。花点时间通读一遍。
    • 谢谢!我到了
    【解决方案2】:

    在指定要连接到的数据库连接字符串后,如何使用连接字符串到实际数据库:

    public class myClassesContext : DbContext 
    { 
        public myClassesContext () 
            : base("connection_string") 
        { 
        } 
    }
    
    <configuration> 
      <connectionStrings> 
        <add name="connection_string" 
             providerName="..." 
             connectionString="..."/> 
      </connectionStrings> 
    </configuration>
    

    【讨论】:

    • 就是这样,这部分我没有问题,我需要弄清楚如何从数据库中获取数据并将其映射到我的类
    • 我们为什么不从数据库中生成模型呢?通过 EDMX? codeproject.com/Articles/739164/…
    猜你喜欢
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多