C# Wcf服务使用实体框架保存对数据库的更改
我正在尝试使用wcf服务和实体框架更新/添加数据。我有两个相同功能的实现。第一个是完美的工作。以下是第一个的代码:C# Wcf服务使用实体框架保存对数据库的更改,c#,wpf,entity-framework,wcf,C#,Wpf,Entity Framework,Wcf,我正在尝试使用wcf服务和实体框架更新/添加数据。我有两个相同功能的实现。第一个是完美的工作。以下是第一个的代码: public bool SaveEmployee(Employee employeeEntity) { Console.WriteLine("SaveEmployee has been executed"); using (var context = new iposEntities()) {
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;
}
第二个实现(不起作用)如下所示:
Visual studio发出错误消息:错误10参数1:无法从“iPos.Interfaces.Employee”转换为“iPos.Service.Employee”C:\Dropbox\UniversalApp\iPos\iPos.Service\PosService.cs 241 51 iPos.Service
请帮帮我,我做错了什么?我相信我的员工POCO课程还可以。这是我的POCO课程
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));
}
}
公共类员工:IEditableObject,INotifyPropertyChanged
{
员工数据备份员工数据;
私人国际雇员ID;
私有字符串国有化;
私有字符串全名;
私人职业职位头衔;
私人字符串标题的城市;
私人可空出生日期;
私人可取消租用权;
私有字符串地址;
私人城市;
私人电话;
私人可为空的报告;
私人可空薪酬;
私有字符串密码;
私人布尔状态;
私人字符串照片;
公共结构EmployeeData
{
内部内部雇员ID;
内部字符串:DNO;
内部字符串全名;
内部职业岗位职称;
内部字符串标题;
内部可空出生日期;
内部可空的有重离子;
内部字符串地址;
内线城市;
内线电话;
内部可为空的报告;
内部可空薪酬;
内部字符串密码;
内部布尔状态;
}
公职人员()
{
}
公共枚举职业职位
{
总雇员,
首席执行官,
收银员,
会计,
先人,
清点主任,
股票经理,
监督人,
安全
供应商经理,
员工经理,
卫生塔夫,
销售代表,
人力资源部,
营销专员,
金融官员,
接待员,
市场部经理,
采购经理,
顾问,
}
公共雇员(int employeeID、字符串NationaldNo、字符串全名、职业职位头衔、字符串职业头衔、可为空的出生日期、可为空的受雇日期、字符串地址、字符串城市、字符串电话、可为空的报告To、可为空的工资、字符串密码、bool状态)
{
this.backupEmplyData=新员工数据();
backupemplyData.employeeID=employeeID;
EmployeeID=EmployeeID;
backupEmplData.nationaldno=nationaldno;
nationaldno=nationaldno;
backupEmplData.fullNames=全名;
全名=全名;
backupEmplData.title=标题;
头衔=头衔;
backupEmplData.titleOfcouresy=titleOfcouresy;
TitleOfcouresy=TitleOfcouresy;
backupEmplData.birthDate=出生日期;
出生日期=出生日期;
backupEmplData.hireDate=hireDate;
HireDate=HireDate;
backupEmplData.address=地址;
地址=地址;
backupEmplData.city=城市;
城市=城市;
backupEmplData.phone=电话;
电话=电话;
backupEmplData.reportsTo=reportsTo;
ReportsTo=ReportsTo;
backupEmplData.password=密码;
密码=密码;
backupEmplData.status=状态;
状态=状态;
}
public void BeginEdit()
{
this.backupemplyData.employeeID=employeeID;
this.backupEmplData.nationaldno=nationaldno;
this.backupEmplData.fullNames=全名;
this.backupEmplData.title=标题;
this.backupEmplData.titleOfcourcey=titleOfcourcey;
this.backupEmplData.birthDate=出生日期;
this.backupEmplData.hireDate=hireDate;
this.backupEmplData.address=地址;
this.backupEmplData.city=城市;
this.backupEmplData.phone=电话;
this.backupEmplData.reportsTo=reportsTo;
this.backupEmplData.salary=薪资;
this.backupEmplData.password=密码;
this.backupEmplData.status=状态;
}
公共作废取消编辑()
{
EmployeeID=this.backupEmployData.EmployeeID;
nationaldno=this.backupEmplData.nationaldno;
FullNames=this.backupEmplData.FullNames;
Title=this.backupEmplData.Title;
TitleOfcourcey=this.backupEmplData.TitleOfcourcey;
BirthDate=this.backupEmplData.BirthDate;
HireDate=this.backupEmplData.HireDate;
地址=this.backupEmplData.Address;
城市=this.backupEmplData.City;
Phone=this.backupEmplData.Phone;
ReportsTo=this.backupEmplData.ReportsTo;
薪水=this.backupEmplData.Salary;
Password=this.backupEmplData.Password;
状态=this.backupEmplData.Status;
}
公共无效EndEdit()
{
this.backupemplyData.employeeID=employeeID;
this.backupEmplData.nationaldno=nationaldno;
this.backupEmplData.fullNames=fullNames;
this.backupEmplData.title=title;
this.backupEmplData.titleOfcourcey=titleOfcourcey;
this.backupEmplData.birthDate=生日;
this.backupEmplData.hireDate=hireDate;
this.backupEmplData.address=地址;
this.backupEmplData.city=city;
this.backupEmplData.phone=phone;
this.backupEmplData.reportsTo=reportsTo;
this.backupEmplData.salary=salary;
this.backupEmplData.password=密码;
this.backupEmplData.status=状态;
}
[数据成员(IsRequired=true)]
[显示(AutoGenerateField=false)]
公共国际雇员ID
{
得到
{
返回员工ID;
}
设置
{
employeeID=值;
NotifyPropertyChanged(“员工ID”);
}
}
[数据成员(IsRequired=true)]
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));
}
}