gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:07:03 28 4
gpt4 key购买 nike

我有一个名为 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com