【问题标题】:Unable to Add/Update using 3-Tier Architecture无法使用 3 层架构添加/更新
【发布时间】:2016-12-27 00:44:53
【问题描述】:

我有一个名为Currency 的表,其中要插入两个属性,即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);
        }
    }

下面是我 currencyBindingSource 在 UI 代码中的创建和绑定方式。

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

【问题讨论】:

  • 如果我们完全不知道currencyBindingSource.Current 是如何创建的或您的上下文是如何设置的,我们怎么知道?对了,为什么DataLayer返回它从参数接收到的实例而BusinessLayer没有返回呢?
  • @CamiloTerevinto currencyBindingSource 通过添加 bindingSource 并将数据源设置为货币来添加。 current 不是 bindingSource 的属性之一吗?我不确定你的最后一个问题。我实际上是从 [youtube.com/watch?v=Ncbr5axCabM]. 引用它的
  • @CamiloTerevinto 我检查了currencyBindingSource.CurrentbtnSave 并意识到currencyBindingSource.Current 只占用Unit
  • 那么您最近的观察解决了您的问题?
  • @RBT nop。我意识到它只需要Unit但我不知道如何解决它。

标签: c# mysql entity-framework devexpress n-tier-architecture


【解决方案1】:

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

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 是在我的应用程序中添加绑定源时创建的数据源数据集的名称。在您的应用程序中可能会有所不同。

希望这会有所帮助!

【讨论】:

  • 谢谢。有效。我没有使用 Dataset,而是将 currencyRow.Unit 替换为 TextEdit,因为添加绑定源时未创建 Dataset。
  • 超级!很高兴它有帮助。谢谢你。您的帖子帮助我了解了 devExpress 控件。
猜你喜欢
  • 2011-07-30
  • 2011-09-30
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2013-04-24
  • 2020-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多