problem to save data in multi layer EF application

  • Thread starter Thread starter zleug
  • Start date Start date
Z

zleug

Guest
Hi All.

When I tried to save a new record I got such error message.

1614469.jpg

I have such scenario in my multi layer application. In Data Layer (DL) I have method in EmployeeFactory class to save record

private static int result;

public static int SaveEmployee(EmployeeDT employee)
{
try
{
using (var context = new Model1())
{
var emp = new Employee
{
EmployeeId = employee.EmployeeId,
Name = employee.Name,
Department = employee.Department,
IsActive = employee.IsActive,
CreatedDate = employee.CreatedDate,
CreatedBy = employee.CreatedBy
};

var result = context.Epmloyees.Add(emp);
context.SaveChanges();
}
return result;
catch Exception(ex)
{
throw new Exception("The data wasn't saved." + ex.ToString());
}
}


In Business Layer (BL) the EmployeeAdapter class has method which pass data from BL to DL

public int SaveEmployee(EmployeeDT employee)
{
return EmployeeFactory.SaveEmployee(employee);
}


And in UI Layer I have click Save button event method

private void SaveEmployee_Click(object sender, RoutedEventArgs e)
{
var employee = new EmployeeDT();

employee.EmployeeId = Guid.NewGuid();
employee.Name = NameTextBox.Text;
employee.Department = DepartmentTextBox.Text;
employee.IsActive = IsActiveCheckBox.IsChecked.HasValue ? IsActiveCheckBox.IsChecked.Value : false;
employee.CreatedDate = DateTime.Now;
employee.CreatedBy = CreatedByTextBox.Text;

var ea = new EmployeeAdapter();
ea.SaveEmployee(employee);
}


I will appreciate if someone explain and show how to fix problem and save record successfully.

Thanks.

Continue reading...
 
Back
Top