Wcf 服务使用 entity framework 保存对数据库的更改
Wcf service saving changes to database using entity framework
我正在尝试 update/add 使用 wcf 服务和 entity framework 的数据。我有 2 个相同功能的实现。第一个工作完美。这是第一个的代码:
public bool SaveEmployee(Employee employeeEntity)
{
Console.WriteLine("SaveEmployee has been executed");
using (var context = new iposEntities())
{
context.Database.Log = Console.Write;
using (var trans = context.Database.BeginTransaction())
{
try
{
if (employeeEntity.EmployeeID == 0)
{
context.employees.Add(new employee()
{
//EmployeeID=employeeEntity.EmployeeID,
NationalIDNo= employeeEntity.NationalIDNo,
FullNames= employeeEntity.FullNames,
Title = employeeEntity.Title.ToString(),
TitleOfCourtesy=employeeEntity.TitleOfCourtesy,
BirthDate=employeeEntity.BirthDate,
HireDate=employeeEntity.HireDate,
Address=employeeEntity.Address,
City=employeeEntity.City,
Phone=employeeEntity.Phone,
ReportsTo=employeeEntity.ReportsTo,
Salary=employeeEntity.Salary,
Password = employeeEntity.Password,
Status = Convert.ToSByte(employeeEntity.Status)
});
}
else
{
employee EmpEntity = context.employees.First(i => i.EmployeeID == employeeEntity.EmployeeID);
EmpEntity.NationalIDNo = employeeEntity.NationalIDNo;
EmpEntity.FullNames = employeeEntity.FullNames;
EmpEntity.Title = employeeEntity.Title.ToString();
EmpEntity.TitleOfCourtesy = employeeEntity.TitleOfCourtesy;
EmpEntity.BirthDate = employeeEntity.BirthDate;
EmpEntity.HireDate = employeeEntity.HireDate;
EmpEntity.Address = employeeEntity.Address;
EmpEntity.City = employeeEntity.City;
EmpEntity.Phone = employeeEntity.Phone;
EmpEntity.ReportsTo = employeeEntity.ReportsTo;
EmpEntity.Salary = employeeEntity.Salary;
EmpEntity.Password = employeeEntity.Password;
EmpEntity.Status = Convert.ToSByte(employeeEntity.Status);
context.Entry(EmpEntity).State = EntityState.Modified;
}
context.SaveChangesAsync();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine("An error occured during saving"+ex.Message);
}
}
}
return true;
}
第二个实现(不起作用)在这里:
public bool SaveEmployee(Employee employeeEntity)
{
Console.WriteLine("SaveEmployee has been executed");
using (var context = new iposEntities())
{
context.Database.Log = Console.Write;
using (var trans = context.Database.BeginTransaction())
{
try
{
if (employeeEntity.EmployeeID == 0)
{
context.employees.Add(employeeEntity);
}
else
{
context.Entry(employeeEntity).State = EntityState.Modified;
}
context.SaveChangesAsync();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine("An error occured during saving"+ex.Message);
}
}
}
return true;
}
这是来自 Visual studio 的错误消息:错误 10 参数 1:无法从 'iPos.Interfaces.Employee' 转换为 'iPos.Service.employee' C:\Dropbox\UniversalApp\iPos\iPos.Service\PosService。 CS 241 51 iPos.Service
请帮助我做错了什么?我相信我的员工 POCO Class 没问题。这是我的 POCO Class
public class Employee : IEditableObject, INotifyPropertyChanged
{
EmployeeData backupEmplData;
private int employeeID;
private string nationalIDNo;
private string fullNames;
private OccupationPositions title;
private string titleOfCourtesy;
private Nullable<System.DateTime> birthDate;
private Nullable<System.DateTime> hireDate;
private string address;
private string city;
private string phone;
private Nullable<int> reportsTo;
private Nullable<float> salary;
private string password;
private bool status;
private string photo;
public struct EmployeeData
{
internal int employeeID;
internal string nationalIDNo;
internal string fullNames;
internal OccupationPositions title;
internal string titleOfCourtesy;
internal Nullable<System.DateTime> birthDate;
internal Nullable<System.DateTime> hireDate;
internal string address;
internal string city;
internal string phone;
internal Nullable<int> reportsTo;
internal Nullable<float> salary;
internal string password;
internal bool status;
}
public Employee()
{
}
public enum OccupationPositions
{
GeneralEmployee,
CEO,
Casheer,
Accountant,
Foreperson,
InventoryOfficer,
StockManager,
supervisor,
Security,
SuppliesManager,
StaffManager,
HygeneStaff,
SalesRepresentative,
HR,
MarketingOfficer,
FinancialOfficer,
Receptionist,
MarketingManager,
PurchasingManager,
Consultant,
}
public Employee(int employeeID, string nationalIDNo, string fullNames, OccupationPositions title, string titleOfCourtesy, Nullable<System.DateTime> birthDate, Nullable<System.DateTime> hireDate, string address, string city, string phone, Nullable<int> reportsTo, Nullable<float> salary, string password, bool status)
{
this.backupEmplData = new EmployeeData();
backupEmplData.employeeID = employeeID;
EmployeeID = employeeID;
backupEmplData.nationalIDNo = nationalIDNo;
NationalIDNo = nationalIDNo;
backupEmplData.fullNames = fullNames;
FullNames = fullNames;
backupEmplData.title = title;
Title = title;
backupEmplData.titleOfCourtesy = titleOfCourtesy;
TitleOfCourtesy = titleOfCourtesy;
backupEmplData.birthDate = birthDate;
BirthDate = birthDate;
backupEmplData.hireDate = hireDate;
HireDate = hireDate;
backupEmplData.address = address;
Address = address;
backupEmplData.city = city;
City = city;
backupEmplData.phone = phone;
Phone = phone;
backupEmplData.reportsTo = reportsTo;
ReportsTo = reportsTo;
backupEmplData.password = password;
Password = password;
backupEmplData.status = status;
Status = status;
}
public void BeginEdit()
{
this.backupEmplData.employeeID = EmployeeID;
this.backupEmplData.nationalIDNo = NationalIDNo;
this.backupEmplData.fullNames = FullNames;
this.backupEmplData.title = Title;
this.backupEmplData.titleOfCourtesy = TitleOfCourtesy;
this.backupEmplData.birthDate = BirthDate;
this.backupEmplData.hireDate = HireDate;
this.backupEmplData.address = Address;
this.backupEmplData.city = City;
this.backupEmplData.phone = Phone;
this.backupEmplData.reportsTo = ReportsTo;
this.backupEmplData.salary = Salary;
this.backupEmplData.password = Password;
this.backupEmplData.status = Status;
}
public void CancelEdit()
{
EmployeeID=this.backupEmplData.employeeID;
NationalIDNo= this.backupEmplData.nationalIDNo;
FullNames= this.backupEmplData.fullNames;
Title= this.backupEmplData.title;
TitleOfCourtesy=this.backupEmplData.titleOfCourtesy;
BirthDate=this.backupEmplData.birthDate;
HireDate=this.backupEmplData.hireDate;
Address=this.backupEmplData.address;
City=this.backupEmplData.city;
Phone=this.backupEmplData.phone;
ReportsTo=this.backupEmplData.reportsTo;
Salary=this.backupEmplData.salary;
Password = this.backupEmplData.password;
Status = this.backupEmplData.status;
}
public void EndEdit()
{
this.backupEmplData.employeeID = EmployeeID;
this.backupEmplData.nationalIDNo=NationalIDNo;
this.backupEmplData.fullNames=FullNames;
this.backupEmplData.title=Title;
this.backupEmplData.titleOfCourtesy=TitleOfCourtesy;
this.backupEmplData.birthDate=BirthDate;
this.backupEmplData.hireDate=HireDate;
this.backupEmplData.address=Address;
this.backupEmplData.city=City;
this.backupEmplData.phone=Phone;
this.backupEmplData.reportsTo=ReportsTo;
this.backupEmplData.salary=Salary;
this.backupEmplData.password=Password;
this.backupEmplData.status = Status;
}
[DataMember(IsRequired = true)]
[Display(AutoGenerateField=false)]
public int EmployeeID
{
get
{
return employeeID;
}
set
{
employeeID = value;
NotifyPropertyChanged("EmployeeID");
}
}
[DataMember(IsRequired = true)]
[Required]
public string NationalIDNo {
get
{
return nationalIDNo;
}
set
{
nationalIDNo = value;
NotifyPropertyChanged("NationalIDNo");
}
}
[DataMember(IsRequired = true)]
[Required]
public string FullNames {
get
{
return fullNames;
}
set
{
fullNames = value;
NotifyPropertyChanged("FullNames");
}
}
[DataMember(IsRequired = true)]
[Required]
[Display(Name = "Position")]
public OccupationPositions Title
{
get
{
return title;
}
set
{
title = value;
NotifyPropertyChanged("Title");
}
}
[DataMember(IsRequired = true)]
public string TitleOfCourtesy {
get
{
return titleOfCourtesy;
}
set
{
titleOfCourtesy = value;
NotifyPropertyChanged("TitleOfCourtesy");
}
}
[DataMember(IsRequired = true)]
[Required]
public Nullable<System.DateTime> BirthDate {
get
{
return birthDate;
}
set
{
birthDate = value;
NotifyPropertyChanged("BirthDate");
}
}
[DataMember(IsRequired = true)]
[Required]
public Nullable<System.DateTime> HireDate {
get
{
return hireDate;
}
set
{
hireDate = value;
NotifyPropertyChanged("HireDate");
}
}
[DataMember(IsRequired = true)]
public string Address {
get
{
return address;
}
set
{
address = value;
NotifyPropertyChanged("Address");
}
}
[DataMember(IsRequired = true)]
public string City {
get
{
return city;
}
set
{
city = value;
NotifyPropertyChanged("City");
}
}
[DataMember(IsRequired = true)]
[Required]
public string Phone {
get
{
return phone;
}
set
{
phone = value;
NotifyPropertyChanged("Phone");
}
}
[DataMember(IsRequired = true)]
[Display(Name="On Contract")]
public bool Status
{
get
{
return status;
}
set
{
status = value;
NotifyPropertyChanged("Status");
}
}
[DataMember(IsRequired = true)]
[Display(AutoGenerateField = false)]
public Nullable<int> ReportsTo {
get
{
return reportsTo;
}
set
{
reportsTo = value;
NotifyPropertyChanged("ReportsTo");
}
}
[DataMember(IsRequired = true)]
public Nullable<float> Salary {
get
{
return salary;
}
set
{
salary = value;
NotifyPropertyChanged("Salary");
}
}
[DataMember(IsRequired = true)]
[Required]
public string Password {
get
{
return password;
}
set
{
password = value;
NotifyPropertyChanged("Password");
}
}
[DataMember(IsRequired = true)]
[Display(AutoGenerateField = false)]
public string Photo
{
get
{
return photo;
}
set
{
photo = value;
NotifyPropertyChanged("Photo");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
看起来您有 2 个版本的 Employee:
'iPos.Interfaces.Employee'
中的一个
另一个在 'iPos.Service.employee'
即使它们使用相同的源代码,对于编译器来说它们也是 2 个不同的 类。
只用一个。
我正在尝试 update/add 使用 wcf 服务和 entity framework 的数据。我有 2 个相同功能的实现。第一个工作完美。这是第一个的代码:
public bool SaveEmployee(Employee employeeEntity)
{
Console.WriteLine("SaveEmployee has been executed");
using (var context = new iposEntities())
{
context.Database.Log = Console.Write;
using (var trans = context.Database.BeginTransaction())
{
try
{
if (employeeEntity.EmployeeID == 0)
{
context.employees.Add(new employee()
{
//EmployeeID=employeeEntity.EmployeeID,
NationalIDNo= employeeEntity.NationalIDNo,
FullNames= employeeEntity.FullNames,
Title = employeeEntity.Title.ToString(),
TitleOfCourtesy=employeeEntity.TitleOfCourtesy,
BirthDate=employeeEntity.BirthDate,
HireDate=employeeEntity.HireDate,
Address=employeeEntity.Address,
City=employeeEntity.City,
Phone=employeeEntity.Phone,
ReportsTo=employeeEntity.ReportsTo,
Salary=employeeEntity.Salary,
Password = employeeEntity.Password,
Status = Convert.ToSByte(employeeEntity.Status)
});
}
else
{
employee EmpEntity = context.employees.First(i => i.EmployeeID == employeeEntity.EmployeeID);
EmpEntity.NationalIDNo = employeeEntity.NationalIDNo;
EmpEntity.FullNames = employeeEntity.FullNames;
EmpEntity.Title = employeeEntity.Title.ToString();
EmpEntity.TitleOfCourtesy = employeeEntity.TitleOfCourtesy;
EmpEntity.BirthDate = employeeEntity.BirthDate;
EmpEntity.HireDate = employeeEntity.HireDate;
EmpEntity.Address = employeeEntity.Address;
EmpEntity.City = employeeEntity.City;
EmpEntity.Phone = employeeEntity.Phone;
EmpEntity.ReportsTo = employeeEntity.ReportsTo;
EmpEntity.Salary = employeeEntity.Salary;
EmpEntity.Password = employeeEntity.Password;
EmpEntity.Status = Convert.ToSByte(employeeEntity.Status);
context.Entry(EmpEntity).State = EntityState.Modified;
}
context.SaveChangesAsync();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine("An error occured during saving"+ex.Message);
}
}
}
return true;
}
第二个实现(不起作用)在这里:
public bool SaveEmployee(Employee employeeEntity)
{
Console.WriteLine("SaveEmployee has been executed");
using (var context = new iposEntities())
{
context.Database.Log = Console.Write;
using (var trans = context.Database.BeginTransaction())
{
try
{
if (employeeEntity.EmployeeID == 0)
{
context.employees.Add(employeeEntity);
}
else
{
context.Entry(employeeEntity).State = EntityState.Modified;
}
context.SaveChangesAsync();
trans.Commit();
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine("An error occured during saving"+ex.Message);
}
}
}
return true;
}
这是来自 Visual studio 的错误消息:错误 10 参数 1:无法从 'iPos.Interfaces.Employee' 转换为 'iPos.Service.employee' C:\Dropbox\UniversalApp\iPos\iPos.Service\PosService。 CS 241 51 iPos.Service
请帮助我做错了什么?我相信我的员工 POCO Class 没问题。这是我的 POCO Class
public class Employee : IEditableObject, INotifyPropertyChanged
{
EmployeeData backupEmplData;
private int employeeID;
private string nationalIDNo;
private string fullNames;
private OccupationPositions title;
private string titleOfCourtesy;
private Nullable<System.DateTime> birthDate;
private Nullable<System.DateTime> hireDate;
private string address;
private string city;
private string phone;
private Nullable<int> reportsTo;
private Nullable<float> salary;
private string password;
private bool status;
private string photo;
public struct EmployeeData
{
internal int employeeID;
internal string nationalIDNo;
internal string fullNames;
internal OccupationPositions title;
internal string titleOfCourtesy;
internal Nullable<System.DateTime> birthDate;
internal Nullable<System.DateTime> hireDate;
internal string address;
internal string city;
internal string phone;
internal Nullable<int> reportsTo;
internal Nullable<float> salary;
internal string password;
internal bool status;
}
public Employee()
{
}
public enum OccupationPositions
{
GeneralEmployee,
CEO,
Casheer,
Accountant,
Foreperson,
InventoryOfficer,
StockManager,
supervisor,
Security,
SuppliesManager,
StaffManager,
HygeneStaff,
SalesRepresentative,
HR,
MarketingOfficer,
FinancialOfficer,
Receptionist,
MarketingManager,
PurchasingManager,
Consultant,
}
public Employee(int employeeID, string nationalIDNo, string fullNames, OccupationPositions title, string titleOfCourtesy, Nullable<System.DateTime> birthDate, Nullable<System.DateTime> hireDate, string address, string city, string phone, Nullable<int> reportsTo, Nullable<float> salary, string password, bool status)
{
this.backupEmplData = new EmployeeData();
backupEmplData.employeeID = employeeID;
EmployeeID = employeeID;
backupEmplData.nationalIDNo = nationalIDNo;
NationalIDNo = nationalIDNo;
backupEmplData.fullNames = fullNames;
FullNames = fullNames;
backupEmplData.title = title;
Title = title;
backupEmplData.titleOfCourtesy = titleOfCourtesy;
TitleOfCourtesy = titleOfCourtesy;
backupEmplData.birthDate = birthDate;
BirthDate = birthDate;
backupEmplData.hireDate = hireDate;
HireDate = hireDate;
backupEmplData.address = address;
Address = address;
backupEmplData.city = city;
City = city;
backupEmplData.phone = phone;
Phone = phone;
backupEmplData.reportsTo = reportsTo;
ReportsTo = reportsTo;
backupEmplData.password = password;
Password = password;
backupEmplData.status = status;
Status = status;
}
public void BeginEdit()
{
this.backupEmplData.employeeID = EmployeeID;
this.backupEmplData.nationalIDNo = NationalIDNo;
this.backupEmplData.fullNames = FullNames;
this.backupEmplData.title = Title;
this.backupEmplData.titleOfCourtesy = TitleOfCourtesy;
this.backupEmplData.birthDate = BirthDate;
this.backupEmplData.hireDate = HireDate;
this.backupEmplData.address = Address;
this.backupEmplData.city = City;
this.backupEmplData.phone = Phone;
this.backupEmplData.reportsTo = ReportsTo;
this.backupEmplData.salary = Salary;
this.backupEmplData.password = Password;
this.backupEmplData.status = Status;
}
public void CancelEdit()
{
EmployeeID=this.backupEmplData.employeeID;
NationalIDNo= this.backupEmplData.nationalIDNo;
FullNames= this.backupEmplData.fullNames;
Title= this.backupEmplData.title;
TitleOfCourtesy=this.backupEmplData.titleOfCourtesy;
BirthDate=this.backupEmplData.birthDate;
HireDate=this.backupEmplData.hireDate;
Address=this.backupEmplData.address;
City=this.backupEmplData.city;
Phone=this.backupEmplData.phone;
ReportsTo=this.backupEmplData.reportsTo;
Salary=this.backupEmplData.salary;
Password = this.backupEmplData.password;
Status = this.backupEmplData.status;
}
public void EndEdit()
{
this.backupEmplData.employeeID = EmployeeID;
this.backupEmplData.nationalIDNo=NationalIDNo;
this.backupEmplData.fullNames=FullNames;
this.backupEmplData.title=Title;
this.backupEmplData.titleOfCourtesy=TitleOfCourtesy;
this.backupEmplData.birthDate=BirthDate;
this.backupEmplData.hireDate=HireDate;
this.backupEmplData.address=Address;
this.backupEmplData.city=City;
this.backupEmplData.phone=Phone;
this.backupEmplData.reportsTo=ReportsTo;
this.backupEmplData.salary=Salary;
this.backupEmplData.password=Password;
this.backupEmplData.status = Status;
}
[DataMember(IsRequired = true)]
[Display(AutoGenerateField=false)]
public int EmployeeID
{
get
{
return employeeID;
}
set
{
employeeID = value;
NotifyPropertyChanged("EmployeeID");
}
}
[DataMember(IsRequired = true)]
[Required]
public string NationalIDNo {
get
{
return nationalIDNo;
}
set
{
nationalIDNo = value;
NotifyPropertyChanged("NationalIDNo");
}
}
[DataMember(IsRequired = true)]
[Required]
public string FullNames {
get
{
return fullNames;
}
set
{
fullNames = value;
NotifyPropertyChanged("FullNames");
}
}
[DataMember(IsRequired = true)]
[Required]
[Display(Name = "Position")]
public OccupationPositions Title
{
get
{
return title;
}
set
{
title = value;
NotifyPropertyChanged("Title");
}
}
[DataMember(IsRequired = true)]
public string TitleOfCourtesy {
get
{
return titleOfCourtesy;
}
set
{
titleOfCourtesy = value;
NotifyPropertyChanged("TitleOfCourtesy");
}
}
[DataMember(IsRequired = true)]
[Required]
public Nullable<System.DateTime> BirthDate {
get
{
return birthDate;
}
set
{
birthDate = value;
NotifyPropertyChanged("BirthDate");
}
}
[DataMember(IsRequired = true)]
[Required]
public Nullable<System.DateTime> HireDate {
get
{
return hireDate;
}
set
{
hireDate = value;
NotifyPropertyChanged("HireDate");
}
}
[DataMember(IsRequired = true)]
public string Address {
get
{
return address;
}
set
{
address = value;
NotifyPropertyChanged("Address");
}
}
[DataMember(IsRequired = true)]
public string City {
get
{
return city;
}
set
{
city = value;
NotifyPropertyChanged("City");
}
}
[DataMember(IsRequired = true)]
[Required]
public string Phone {
get
{
return phone;
}
set
{
phone = value;
NotifyPropertyChanged("Phone");
}
}
[DataMember(IsRequired = true)]
[Display(Name="On Contract")]
public bool Status
{
get
{
return status;
}
set
{
status = value;
NotifyPropertyChanged("Status");
}
}
[DataMember(IsRequired = true)]
[Display(AutoGenerateField = false)]
public Nullable<int> ReportsTo {
get
{
return reportsTo;
}
set
{
reportsTo = value;
NotifyPropertyChanged("ReportsTo");
}
}
[DataMember(IsRequired = true)]
public Nullable<float> Salary {
get
{
return salary;
}
set
{
salary = value;
NotifyPropertyChanged("Salary");
}
}
[DataMember(IsRequired = true)]
[Required]
public string Password {
get
{
return password;
}
set
{
password = value;
NotifyPropertyChanged("Password");
}
}
[DataMember(IsRequired = true)]
[Display(AutoGenerateField = false)]
public string Photo
{
get
{
return photo;
}
set
{
photo = value;
NotifyPropertyChanged("Photo");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
看起来您有 2 个版本的 Employee:
'iPos.Interfaces.Employee'
中的一个
另一个在 'iPos.Service.employee'
即使它们使用相同的源代码,对于编译器来说它们也是 2 个不同的 类。
只用一个。