我在.Net中创建了一个简单的应用程序,它有一个可以对数据库执行CRUD操作的服务(添加,删除,获取)。
我的问题是,当我从存储库调用一个方法,如“添加一个新对象”时,操作执行没有任何问题但是当我调用向数据库添加新对象的服务方法时,它运行大约一个分钟什么都不做,然后我得到以下例外:
EntityFramework.dll中发生了'System.Data.SqlClient.SqlException'类型的异常但未在用户代码中处理
其他信息:建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)
来自服务的方法只是调用存储库来添加该对象。
以下是我调用服务的代码(所有这些都有这个问题):
using DataLayer.Entities;
using DataLayer.Implementation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace WfcConsumer
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.ClientServiceClient clientService = new ServiceReference1.ClientServiceClient();
ServiceReference2.ProductServiceClient productService = new ServiceReference2.ProductServiceClient();
ServiceReference3.StoreServiceClient storeService = new ServiceReference3.StoreServiceClient();
ServiceReference5.SupplierServiceClient supplierService = new ServiceReference5.SupplierServiceClient();
Store store = new Store();
Product product = new Product();
Supplier supplier = new Supplier();
Client client = new Client();
client.Username = "test";
client.Parola = "123";
client.Email = "abcd@yahoo.com";
client.clientId = 2;
store.Name = "amazon";
store.StoreId = 1;
product.Name = "laptop";
product.ProductId = 3;
product.Price = 50000;
Console.WriteLine("the begining");
supplier.categorie = "IT";
supplier.name = "walmart";
supplier.SupplierId = 2;
Console.WriteLine("client");
clientService.Add(client);
Console.WriteLine("product");
productService.Add(product);
Console.WriteLine("store");
storeService.Add(store);
Console.WriteLine("supplier");
supplierService.Add(supplier);
Console.WriteLine("The end");
}
}
}
我使用的服务类之一:
using DataLayer;
using DataLayer.Entities;
using DataLayer.Implementation;
using HelloWCF.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HelloWCF.Implementation
{
public class ClientService : IClientService
{
public ClientRepository c = new ClientRepository();
public void Add(Client obj)
{
c.Add(obj);
}
public void Delete(int id)
{
c.Delete(id);
}
public IEnumerable<Client> GetAll()
{
return c.GetAll();
}
public Client GetById(int id)
{
return c.GetById(id);
}
public void Update(int id, Client obj)
{
c.Update(id, obj);
}
}
}
以上服务的存储库类:
using DataLayer.Entities;
using DataLayer.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataLayer.Implementation
{
public class ClientRepository : IClientRepository
{
ApplicationContext db = new ApplicationContext();
public void Add(Client obj)
{
db.Clients.Add(obj);
db.SaveChanges();
}
public void Delete(int id)
{
var obj = db.Clients.ElementAt(id);
db.Clients.Remove(obj);
db.SaveChanges();
}
public IEnumerable<Client> GetAll()
{
return db.Clients.ToList();
}
public Client GetById(int id)
{
return db.Clients.ElementAt(id);
}
public void Update(int id, Client obj)
{
var _obj = db.Clients.ElementAt(id);
_obj.clientId = obj.clientId;
_obj.Email = obj.Email;
_obj.Nume = obj.Nume;
_obj.Parola = obj.Parola;
_obj.Store = obj.Store;
_obj.Username = obj.Username;
db.SaveChanges();
}
}
}
}
当我直接调用存储库时,你知道为什么它工作正常但是当我调用服务时它会给我这个例外吗?
提前致谢!