2016-12-27 47 views
1

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

下面是我的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代码约束。

  1. 从工具箱添加bindingSource。
  2. 转至属性 - >数据源 - >添加项目数据源 - >对象 - >选择currency表。
  3. 添加两个文本框 - >属性 - > DataBindings - > EditValue - >从currencyBindingSource中选择UnitRate
+0

我们怎么知道我们是否完全不知道currencyBindingSource.Current是如何创建的或者您的上下文是如何设置的?顺便说一下,为什么DataLayer返回从参数接收到的实例,并且BusinessLayer不返回它? –

+0

@CamiloTerevinto currencyBindingSource是通过添加bindingSource并将数据源设置为货币添加的。不是'当前'bindingSource的属性之一吗?我不确定你最后的问题。我实际上从[https://www.youtube.com/watch?v=Ncbr5axCabM]参考它。 – active92

+0

@CamiloTerevinto我在'btnSave'处检查了'currencyBindingSource.Current',并意识到'currencyBindingSource.Current'只是'Unit'。 – active92

回答

0

这是你需要做的。我相信你缺少这些需要一些适当的强制转换:

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是得到了,而在我的应用程序添加绑定源创建数据源的数据集的名称。它可能在你的应用程序中有所不同。

希望这会有所帮助!

+0

谢谢。有效。我没有使用数据集,而是用TextEdit替换'currencyRow.Unit',因为在添加我的绑定源时未创建数据集。 – active92

+0

超级!很高兴它有帮助。并感谢你。你的文章帮助我了解了devExpress控件。 – RBT

相关问题