无法 Add/Update 使用 3 层架构

Unable to Add/Update using 3-Tier Architecture

我有一个名为 Currency 的 table,其中要插入两个属性,即 UnitRate

当我按addedit时,只有Unit被保存,Rate仍然是0

当我按下delete时,记录删除成功

下面是来自数据层

的代码
public interface ICurrencyRepository
{
    List<Currency> GetAll();

    Currency GetById(int id);

    Currency Insert(Currency obj);

    void Update(Currency obj);

    void Delete(Currency obj);
}

public class CurrencyRepository : ICurrencyRepository
{
    public void Delete(Currency obj)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            db.Currencies.Attach(obj);
            db.Currencies.Remove(obj);
            db.SaveChanges();
        }
    }

    public List<Currency> GetAll()
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            return db.Currencies.ToList();
        }
    }

    public Currency GetById(int id)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            return db.Currencies.Find(id);
        }
    }

    public Currency Insert(Currency obj)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            db.Currencies.Add(obj);
            db.SaveChanges();
            return obj;
        }
    }

    public void Update(Currency obj)
    {
        using (ApplicationDbContext db = new ApplicationDbContext())
        {
            db.Currencies.Attach(obj);
            db.Entry(obj).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();
        }
    }
}

下面是来自业务层

的代码
public static class CurrencyServices
{
    static ICurrencyRepository repository;

    static CurrencyServices()
    {
        repository = new CurrencyRepository();
    }

    public static List<Currency> GetAll()
    {
        return repository.GetAll();
    }

    public static Currency GetById(int id)
    {
        return repository.GetById(id);
    }

    public static Currency Insert(Currency obj)
    {
        return repository.Insert(obj);
    }

    public static void Update(Currency obj)
    {
        repository.Update(obj);
    }

    public static void Delete(Currency obj)
    {
        repository.Delete(obj);
    }
}

下面是我的代码 UI(带网格的页面)

    private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        DocumentController.ActivateForm(typeof(Test), null);
        currencyBindingSource.DataSource = CurrencyServices.GetAll();
    }

    private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        if (currencyBindingSource.Current == null)
        {
            return;
        }

        else
        {
            DocumentController.ActivateForm(typeof(Test), currencyBindingSource.Current as Currency);
            currencyBindingSource.DataSource = CurrencyServices.GetAll();
        }
    }

下面是我的 UI(编辑页面)

中的代码
    bool isNew;
    public CurrencyEdit(Currency obj)
    {
        InitializeComponent();
        if (obj == null)
        {
            currencyBindingSource.DataSource = new Currency();
            isNew = true;
        }

        else
        {
            currencyBindingSource.DataSource = obj;
            isNew = false;
        }
    }

    private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
    {
        if (isNew)
        {
            CurrencyServices.Insert(currencyBindingSource.Current as Currency);
        }

        else
        {
            CurrencyServices.Update(currencyBindingSource.Current as Currency);
        }
    }

下面是我如何在 UI 代码中创建和绑定 currencyBindingSource

  1. 从工具箱添加绑定源。
  2. 转到属性 -> 数据源 -> 添加项目数据源 -> 对象 -> 选择 currency table。
  3. 添加两个文本框 -> 属性 -> DataBindings -> EditValue -> 从 currencyBindingSource 中选择 UnitRate

这是您需要做的。我相信您缺少一些必需的适当演员表:

private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    EfTestDataSet.CurrencyRow currencyRow = (EfTestDataSet.CurrencyRow)  ( currencyBindingSource.Current as DataRowView).Row;

    if (isNew)
    {
        CurrencyServices.Insert(new Currency { Unit = currencyRow.Unit, Rate = currencyRow.Rate } );
    }
    else
    {
        CurrencyServices.Update(new Currency { Unit = currencyRow.Unit, Rate = currencyRow.Rate } );
    }
}

注意EfTestDataSet是在我的应用程序中添加绑定源时创建的数据源数据集的名称。在您的应用程序中可能有所不同。

希望对您有所帮助!