无法 Add/Update 使用 3 层架构
Unable to Add/Update using 3-Tier Architecture
我有一个名为 Currency
的 table,其中要插入两个属性,即 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);
}
}
下面是我如何在 UI 代码中创建和绑定 currencyBindingSource
。
- 从工具箱添加绑定源。
- 转到属性 -> 数据源 -> 添加项目数据源 -> 对象 -> 选择
currency
table。
- 添加两个文本框 -> 属性 -> DataBindings -> EditValue -> 从
currencyBindingSource
中选择 Unit
和 Rate
。
这是您需要做的。我相信您缺少一些必需的适当演员表:
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
是在我的应用程序中添加绑定源时创建的数据源数据集的名称。在您的应用程序中可能有所不同。
希望对您有所帮助!
我有一个名为 Currency
的 table,其中要插入两个属性,即 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);
}
}
下面是我如何在 UI 代码中创建和绑定 currencyBindingSource
。
- 从工具箱添加绑定源。
- 转到属性 -> 数据源 -> 添加项目数据源 -> 对象 -> 选择
currency
table。 - 添加两个文本框 -> 属性 -> DataBindings -> EditValue -> 从
currencyBindingSource
中选择Unit
和Rate
。
这是您需要做的。我相信您缺少一些必需的适当演员表:
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
是在我的应用程序中添加绑定源时创建的数据源数据集的名称。在您的应用程序中可能有所不同。
希望对您有所帮助!