简体   繁体   English

使用DataAccessService MVVM Light更新数据库

[英]Update database using DataAccessService MVVM Light

This has been bugging me for a while now, what I would like to be able to do is update an existing record on the database by making a change to the ObservableCollection or the record class SquirrelDataGridActiveView . 这已经困扰了我一段时间,我想要做的是通过更改ObservableCollection或记录类SquirrelDataGridActiveView来更新数据库中的现有记录。
This is my DataAccessService: 这是我的DataAccessService:

public interface IDataAccessService
{
    ObservableCollection<SquirrelDataGridActiveView> GetEmployees();
    void UpdateRecord(SquirrelDataGridActiveView Emp);
    int CreateEmployee(SquirrelDataGridActiveView Emp);
}

/// <summary>
/// Class implementing IDataAccessService interface and implementing
/// its methods by making call to the Entities using CompanyEntities object
/// </summary>
public class DataAccessService : IDataAccessService
{
    Drive_SHEntities context;
    public DataAccessService()
    {
        context = new Drive_SHEntities();
    }
    public ObservableCollection<SquirrelDataGridActiveView> GetEmployees()
    {
        ObservableCollection<SquirrelDataGridActiveView> Employees = new ObservableCollection<SquirrelDataGridActiveView>();
        foreach (var item in context.SquirrelDataGridActiveViews)
        {
            Employees.Add(item);
        }
        return Employees;
    }

    public int CreateEmployee(SquirrelDataGridActiveView Emp)
    {
        context.SquirrelDataGridActiveViews.Add(Emp);
        context.SaveChanges();
        return Emp.ID;
    }

    public void UpdateRecord(SquirrelDataGridActiveView temp)
    {

    }
}

As you can see there is already a GetEmployees() method and a CreateEmployee() method however I'm finiding it very difficult to update the database with the new values. 如您所见,已经有一个GetEmployees()方法和一个CreateEmployee()方法,但是我认为用新值更新数据库非常困难。

Any suggestions would be much appreciated. 任何建议将不胜感激。

Looks like the problem maybe be in your EF layer. 看来问题出在您的EF层中。 Do you have any unit tests to check that the creation of a new Employee record works properly? 您是否有任何单元测试来检查新员工记录的创建是否正常进行? Can you break into the CreateEmployee and inspect the Emp.ID field? 您可以闯入CreateEmployee并检查Emp.ID字段吗? See if it's set to something. 看看是否设置为某些东西。 If it is, the creation of the new record works properly. 如果是这样,则新记录的创建将正常进行。 One observation, if I may: your data access service does not need to return an ObservableCollection. 一个观察,如果可能的话:您的数据访问服务不需要返回ObservableCollection。 I assume you already have an observable collection in your view-model class. 我假设您在视图模型类中已经有一个可观察的集合。 If you want to keep alive the binding between the data grid in the view and the observable collection property in the view-model class, you should not reassign the OC property of the view-model class. 如果要保持视图中的数据网格与视图模型类中的observable集合属性之间的绑定有效,则不应重新分配视图模型类的OC属性。 The pattern you should follow is this: 您应该遵循的模式是这样的:

  • return an IEnumeration from your data access service's GetEmployees() method; 从您的数据访问服务的GetEmployees()方法返回IEnumeration;
  • Optional, but recommended is to make GetEmployees awaitable; 可选,但建议使GetEmployees处于等待状态;
  • in your view-model class, clear the content of the OC property and then add, one by one, all the items in the IEnumeration collection returned by your GetEmployees() method. 在您的视图模型类中,清除OC属性的内容,然后一个一个地添加GetEmployees()方法返回的IEnumeration集合中的所有项目。 This way your binding will continue to work and any updates in the database will be reflected in the view's data grid. 这样,绑定将继续起作用,并且数据库中的所有更新将反映在视图的数据网格中。

You can keep the OC return from your GetEmployees, but in your view- model you still need to transfer all the items, one by one, into the property that is, most likely, bound to the data grid in the view. 您可以保留来自GetEmployees的OC返回,但是在您的视图模型中,您仍然需要将所有项目一个接一个地转移到最有可能绑定到视图数据网格的属性中。 If you just assign the GetEmployees() returned OC list to the the view-model's property, your binding will be gone. 如果仅将GetEmployees()返回的OC列表分配给视图模型的属性,则绑定将消失。

Perhaps your data grid does not refresh properly and that's why you conclude that the database is not updated when in fact it is. 也许您的数据网格无法正确刷新,这就是为什么您认为数据库实际上没有更新的原因。

HTH, Eddie HTH,埃迪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM