【发布时间】:2016-12-27 00:44:53
【问题描述】:
我有一个名为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表。 - 添加两个文本框->属性->数据绑定->编辑值->从
currencyBindingSource中选择Unit和Rate。
【问题讨论】:
-
如果我们完全不知道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