Entity Framework 6 error serverversion: (System.Data.SqlClient.SqlConnection) clientOrderContext.Database.Connection) .ServerVersion

I am implementing a web api that will retrieve data using the framework 6 entity. I use Sql Server 2014 and visual studio 2015. If I debug the code in the CustomerDao class, I see an exception in the customerOrderContext object, although I can see the entries in the client object. However, after executing the use block, I do not see any entries.

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

The connection string in the configuration file is as follows

 <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" /> 

Context class follows

 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; } 
+6
source share
1 answer

The exception is documented , because the connection is closed. There are no problems with this.

But it is obvious that you get an empty result because you did not use the result, you simply return new List<BusinessObjects.Customers>() in your method:

 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 } 

You need to return dataAccess.CustomerDao.GetAllCustomers(); :

+6
source

Source: https://habr.com/ru/post/1011961/


All Articles