实体框架6错误serverversion:(System.Data.SqlClient.SqlConnection)customerOrderContext.Database.Connection).ServerVersion

时间:2016-11-02 22:44:49

标签: c# .net entity-framework asp.net-web-api entity-framework-6

我正在实现一个使用实体框架6获取数据的web api。我正在使用Sql Server 2014和visual studio 2015.While调试CustomerDao类中的代码我在customerOrderContext对象中看到一个异常虽然我可以查看客户对象中的记录。但是,在执行使用块之后,我无法看到任何记录。

((System.Data.SqlClient.SqlConnection)customerOrderContext.Database.Connection).ServerVersion

CustomerDao

using (var customerOrderContext = new Entities())
                {
                    return (from customer in customerOrderContext.Customers

                            select new CustomerOrder.BusinessObjects.Customers
                            {
                                Id = customer.Id,
                                FirstName = customer.FirstName,
                                LastName = customer.LastName,
                                Address = customer.Address,
                                City = customer.City,
                                Email = customer.Email,
                                Gender = customer.Gender,
                                State = customer.State,
                                Zip = customer.Zip
                            }).ToList();
                }

配置文件中的连接字符串如下

<add name="Entities" connectionString="metadata=res://*/EF.CustomerOrderContext.csdl|res://*/EF.CustomerOrderContext.ssdl|res://*/EF.CustomerOrderContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Tom-PC\MSSQLSERVER2014;initial catalog=Ransang;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

上下文类如下

public partial class Entities : DbContext
{
    public Entities()
        : base("name=Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

    }

    public virtual DbSet<Customer> Customers { get; set; }
    public virtual DbSet<OrderDetail> OrderDetails { get; set; }
    public virtual DbSet<Order> Orders { get; set; }
    public virtual DbSet<Product> Products { get; set; }
}

enter image description here

CustomProvider.cs

  public IEnumerable<BusinessObjects.Customers> GetAllCustomers()
        {
            IList<BusinessObjects.Customers> customerCollection = new List<BusinessObjects.Customers>();
            dataAccess.CustomerDao.GetAllCustomers();
            return customerCollection;
        }

1 个答案:

答案 0 :(得分:6)

例外是documented,这是因为连接已关闭。这没有问题。

但显然你得到空结果,因为你没有使用结果,你只是在你的方法中返回new List<BusinessObjects.Customers>()

public IEnumerable<BusinessObjects.Customers> GetAllCustomers()
{
    IList<BusinessObjects.Customers> customerCollection = 
        new List<BusinessObjects.Customers>(); // ← An empty list
    dataAccess.CustomerDao.GetAllCustomers();  // ← Just executed but didn't use anywhere
    return customerCollection;                 // ← The empty list you created at first
}

您需要return dataAccess.CustomerDao.GetAllCustomers();