Sql插件无法使用WPF

时间:2016-12-07 18:45:57

标签: wpf entity-framework

我使用EF。我的分层结构是这样的:

  • 表示层:WPF桌面项目。
  • 数据层:包含mdf和edmx。
  • 业务层:连接数据库的服务层。

enter image description here

当我插入时:

表示层:

private void Button_Click_3(object sender, RoutedEventArgs e)
{        
    dealer_detail.Telephone = textTelephone.Text;
    dealer_detail.Abstract = textAbstract.Text;
    dealer_detail.Address = textAddress.Text;
    dealer_detail.CreatedDate = dealerDatePicker.SelectedDate;
    dealer_detail.Fax = textFax.Text;
    dealer_detail.Name = textName.Text;

    dealer.Email = textEmail.Text;
    dealer.Password = textPass.Text;

    var result = conn.CreateNewDealer(dealer, dealer_detail);

    if (result > 0)
    {
        MessageBox.Show("Yeni Bayii Oluşturuldu! \n Kayıt No:"+result);
    }
    else
    {
        MessageBox.Show("Bayi Oluştururken Hata Oluştu!");
    }
}

业务层:

public int CreateNewDealer(DEALER dealer, DEALER_DETAIL dealer_detail)
{
    try
    {
        _db.DEALER_DETAIL.Add(dealer_detail);
        _db.SaveChanges();
    }
    catch (Exception)
    {                
        throw;
    }

    return dealer.Id;
}

一开始,表格为空:

enter image description here

我添加了数据,然后选择了。似乎有一行数据。

enter image description here

但表格中没有数据。

1 个答案:

答案 0 :(得分:1)

IT看起来您需要首先在数据库中创建交易,然后使用经销商的dealer.ID创建dealer_detail。然后返回ID。

var newDealer = _db.DEALER.Add(dealer);
dealer_detail.DealerID = newDealer.ID;
_db.DEALER_DETAIL.Add(dealer_detail);
_db.SaveChanges();

然后返回newDealer.Id;