我有一个名为Currency
的表,其中有两个要插入的属性,即Unit
和Rate
。无法使用三层体系结构添加/更新
当我按下add
或edit
,只有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代码约束。
- 从工具箱添加bindingSource。
- 转至属性 - >数据源 - >添加项目数据源 - >对象 - >选择
currency
表。 - 添加两个文本框 - >属性 - > DataBindings - > EditValue - >从
currencyBindingSource
中选择Unit
和Rate
。
我们怎么知道我们是否完全不知道currencyBindingSource.Current是如何创建的或者您的上下文是如何设置的?顺便说一下,为什么DataLayer返回从参数接收到的实例,并且BusinessLayer不返回它? –
@CamiloTerevinto currencyBindingSource是通过添加bindingSource并将数据源设置为货币添加的。不是'当前'bindingSource的属性之一吗?我不确定你最后的问题。我实际上从[https://www.youtube.com/watch?v=Ncbr5axCabM]参考它。 – active92
@CamiloTerevinto我在'btnSave'处检查了'currencyBindingSource.Current',并意识到'currencyBindingSource.Current'只是'Unit'。 – active92