У меня есть база данных из нескольких таблиц, которая реализуется службой WCF.
Здесь я создаю базу данных и работаю в первой таблице.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data;
using System.Data.Entity;
using WcfRestFullService.Model;
namespace WcfRestFullService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerSevice" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select CustomerSevice.svc or CustomerSevice.svc.cs at the Solution Explorer and start debugging.
public class CustomerSevice : ICustomerSevice
{
MySQLEntities dc;
public CustomerSevice()
{
dc = new MySQLEntities();
}
public List<customer> GetAllCustomer()
{
var query = (from a in dc.customers
select a).Distinct().Include(c => c.customerpreference);
List<customer> CustomersList = new List<customer>();
query.ToList().ForEach(x =>
{
CustomersList.Add(new customer
{
Id_Cus = x.Id_Cus,
FirstName_Cus = x.FirstName_Cus,
LastName_Cus = x.LastName_Cus,
PhoneNum_Cus = x.PhoneNum_Cus,
Email_Cus = x.Email_Cus,
});
});
return CustomersList;
}
public customer CustomerDetails(string Id_Cus)
{
customer Cust = new customer();
try
{
var query = (from a in dc.customers
where a.Id_Cus.Equals(Id_Cus)
select a).Distinct().FirstOrDefault();
Cust.Id_Cus = query.Id_Cus;
Cust.FirstName_Cus = query.FirstName_Cus;
Cust.LastName_Cus = query.LastName_Cus;
Cust.PhoneNum_Cus = query.PhoneNum_Cus;
Cust.Email_Cus = query.Email_Cus;
}
catch (Exception ex)
{
throw new FaultException<string>(ex.Message);
}
return Cust;
}
// DELETE
public void DeleteCustomer(string Id_Cus)
{
//MySQLEntities Cust = new MySQLEntities(); //check the file Model.edmx->ModelContext.tt->MySQLEntitys
int k = Convert.ToInt32(Id_Cus);
customer cur = (from n in dc.customers
where n.Id_Cus == k
select n).Include(c => c.customerpreference).Include(c => c.orders).Include(c => c.dishesrankings).ToList().First();
dc.Configuration.ValidateOnSaveEnabled = false;
dc.customers.Remove(cur);
dc.SaveChanges();
}
//Insert/POST
public void InsertCustomer(customer customerDataContract)
{
//MySQLEntities Cust = new MySQLEntities();
customer cust = new customer();
{
//cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
cust.FirstName_Cus = customerDataContract.FirstName_Cus;
cust.LastName_Cus = customerDataContract.LastName_Cus;
cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);
cust.Email_Cus = customerDataContract.Email_Cus;
//}
Dictionary<string, int> dict = dc.customers.GroupBy(x => x.Email_Cus, y => y.Id_Cus)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
dc.customers.Add(cust);
//string p = "Vasa";
customerpreference Cus_Pref = new customerpreference()
{
Id_Cus = customerDataContract.Id_Cus,
Id_Res = 1, // some value
Name_Dis = "lion", // some value
Id_Type = 1 // some value
};
dc.customerpreferences.Add(Cus_Pref);
dc.SaveChanges();
int k = Convert.ToInt32(cust.Id_Cus);
customer custFromDb = (from n in dc.customers
where n.Id_Cus == k
select n).Include(c => c.customerpreference).Include(c=>c.orders).Include(c=>c.dishesrankings).First();
}
}
//Update/PUT
public void UpdateCustomer(customer customerDataContract)
{
//using (CustomerDataContract Cust = new CustomerDataContract())
//using (MySQLEntities Cust = new MySQLEntities())
int k = Convert.ToInt32(customerDataContract.Id_Cus);
customer cust = dc.customers.Where(n => n.Id_Cus == k).FirstOrDefault();
cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
cust.FirstName_Cus = customerDataContract.FirstName_Cus;
cust.LastName_Cus = customerDataContract.LastName_Cus;
cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);
cust.Email_Cus = customerDataContract.Email_Cus;
dc.SaveChanges();
}
}
}
Обратите внимание на строку:
MySQLEntities dc;
public CustomerSevice()
{
dc = new MySQLEntities();
}
После этого я начинаю работать с другой таблицей:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;
using WcfRestFullService.Model;
namespace WcfRestFullService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerPreferences" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select CustomerPreferences.svc or CustomerPreferences.svc.cs at the Solution Explorer and start debugging.
public class CustomerPreferences : ICustomerPreferences, IDisposable
{
MySQLEntities dc; //LIKE This
string connStr = "server=localhost;user=root;port=3306;database=chik-chak;password=l10ksfnq5h2c";
public CustomerPreferences()
{
// dc = new MySQLEntities(); //Perhaps the problem is that I'm trying to create a new database, so the data is not imported from the first table, do I need these lines of code ??
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
}
public customerpreference CustomerDetails(string Id_Cus)
{
customerpreference Cust = new customerpreference();
try
{
var query = (from a in dc.customerpreferences
where a.Id_Cus.Equals(Id_Cus)
select a).Distinct().FirstOrDefault();
Cust.Id_Cus = query.Id_Cus;
Cust.Id_Res = query.Id_Res;
Cust.Name_Dis = query.Name_Dis;
Cust.Id_Type = query.Id_Type;
}
catch (Exception ex)
{
throw new FaultException<string>(ex.Message);
}
return Cust;
}
public void DeleteCustomerPreference(string Id_Cus)
{
int k = Convert.ToInt32(Id_Cus);
customerpreference cur = (from n in dc.customerpreferences
where n.Id_Cus == k
select n).ToList().First();
dc.Configuration.ValidateOnSaveEnabled = false;
dc.customerpreferences.Remove(cur);
dc.SaveChanges();
}
public List<customerpreference> GetAllCustomerPreferences()
{
//MySqlConnection conn = DBUtils.GetDBConnection();
//conn.Open();
//MySqlCommand dc = new MySqlCommand();
//dc.Connection = conn;
// MySQLEntities dc;
var query = (from a in dc.customerpreferences
select a).Distinct();
List<customerpreference> CustomersList = new List<customerpreference>();
query.ToList().ForEach(x =>
{
CustomersList.Add(new customerpreference
{
Id_Cus = x.Id_Cus,
Id_Res = x.Id_Res,
Name_Dis = x.Name_Dis,
Id_Type = x.Id_Type,
});
});
//dc.Connection = conn;
//dc.CommandText = query.ToString();
//conn.Close();
return CustomersList;
}
public void InsertCustomerPreference(customerpreference customerDataContractpreference)
{
customerpreference cust = new customerpreference();
string query = "INSERT INTO tb_customerpreferences VALUES (@Id_Cus, @Id_Res, @Name_Dis,@Id_Type);";
MySqlCommand command = new MySqlCommand(query, connStr);
command.Parameters.Add(new MySqlParameter()
{
cust.Id_Cus = customerDataContractpreference.Id_Cus;
cust.Id_Res = customerDataContractpreference.Id_Res;
cust.Name_Dis = customerDataContractpreference.Name_Dis;
cust.Id_Type = customerDataContractpreference.Id_Type; }
);
dc.customerpreferences.Add(cust);
dc.SaveChanges();
}
public void UpdateCustomerPreference(customerpreference customerDataContractpreference)
{
int k = customerDataContractpreference.Id_Cus;
customerpreference cust = dc.customerpreferences.Where(n => n.Id_Cus == k).FirstOrDefault();
cust.Id_Cus = customerDataContractpreference.Id_Cus;
cust.Id_Res = customerDataContractpreference.Id_Res;
cust.Name_Dis = customerDataContractpreference.Name_Dis;
cust.Id_Type = customerDataContractpreference.Id_Type;
dc.SaveChanges();
}
}
}
обратите внимание на строку:
MySQLEntities dc; //LIKE This
string connStr = "server=localhost;user=root;port=3306;database=chik-chak;password=l10ksfnq5h2c";
public CustomerPreferences()
{
// dc = new MySQLEntities(); //Perhaps the problem is that I'm trying to create a new database, so the data is not imported from the first table, do I need these lines of code ??
MySqlConnection conn = new MySqlConnection(connStr);
conn.Open();
}
Я пытался реализовать соединение с базой данных только в Вставьте метод
public void InsertCustomerPreference(customerpreference customerDataContractpreference)
{
customerpreference cust = new customerpreference();
string query = "INSERT INTO tb_customerpreferences VALUES (@Id_Cus, @Id_Res, @Name_Dis,@Id_Type);";
MySqlCommand command = new MySqlCommand(query, connStr);
command.Parameters.Add(new MySqlParameter()
{
cust.Id_Cus = customerDataContractpreference.Id_Cus;
cust.Id_Res = customerDataContractpreference.Id_Res;
cust.Name_Dis = customerDataContractpreference.Name_Dis;
cust.Id_Type = customerDataContractpreference.Id_Type; }
);
dc.customerpreferences.Add(cust);
dc.SaveChanges();
}
, и в этой и следующей строках есть ошибка, когда я добавляю данные в базу данных
MySqlCommand command = new MySqlCommand(query, conn);
//or I tried
MySqlCommand command = new MySqlCommand(query, connStr);
//Here problem too
command.Parameters.Add(new MySqlParameter()
{
cust.Id_Cus = customerDataContractpreference.Id_Cus;
cust.Id_Res = customerDataContractpreference.Id_Res;
cust.Name_Dis = customerDataContractpreference.Name_Dis;
cust.Id_Type = customerDataContractpreference.Id_Type; }
);
Не могли бы вы сказать, что я делаю не так?