我正在尝试使用wcf服务和实体框架更新/添加数据。我有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课程还可以。这是我的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));
}
}
}
答案 0 :(得分:0)
看起来你有两个版本的员工:
一个在'iPos.Interfaces.Employee'中
另一个在'iPos.Service.employee'中
即使他们使用相同的源代码,对于编译器,它们也是2个不同的类 只使用一个。