c# - 无法使用 3 层架构添加/更新

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

我有一个名为 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();
        }
    }

下面是来 self 的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 代码中创建和绑定(bind) currencyBindingSource

  1. 从工具箱中添加 bindingSource。
  2. 转到属性 -> 数据源 -> 添加项目数据源 -> 对象 -> 选择 currency 表。
  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 是在我的应用程序中添加绑定(bind)源时创建的数据源数据集的名称。在您的应用程序中可能有所不同。

希望这对您有所帮助!

关于c# - 无法使用 3 层架构添加/更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337694/

相关文章:

php - 使用 php 构建时间间隔值数组

c# - 如何在 Entity Framework + SQL Server 中强制执行一对一关系

c# - CS0411 : The type arguments for method 'System.Web.Mvc.Html.EditorExtensions.EditorFor<>)' cannot be inferred from the usage

c# - 使用 ENV 变量在 appsettings.json 中设置键 - ASP.NET Core 3.1 Docker

php - 如何将海量 JSON 对象导入 MySQL

mysql - 使用 COUNT() 和 GROUP BY 进行内连接

c# - 检查组件是否在最外层的 LifetimeScope 中解析

java - 约会的数据模型 - 选择多个日期

c# - 如何编写自己的基于XmlService 的ORM?

c# - Linq to EF 系统不支持异常 System.NotSupportedException