实体框架按记录获取实体ID

时间:2015-04-11 10:41:37

标签: c# winforms entity-framework

我有一个用户保存新房产的表格。在该表单中,他可以从组合框中选择已在客户端表中注册的客户端名称。拥有该遗产的新房地产和客户ID的ID将保存在房地产表中。问题是我无法弄清楚如何通过组合框中的选定客户端名称获取ID。

private void Form4_Load(object sender, EventArgs e)
    {
        SelectClientcomboBox.DataSource = AgencyContext.Client.ToList();
        SelectClientcomboBox.DisplayMember = "ClientName";
        SelectClientcomboBox.Invalidate();
    }
    private void SaveEstateButton_Click(object sender, EventArgs e)
    {
        Estate estate = new Estate();
        estate.ClientID =
    }

1 个答案:

答案 0 :(得分:0)

在Form_Load中绑定值成员属性

private void Form4_Load(object sender, EventArgs e)
{
    SelectClientcomboBox.DataSource = AgencyContext.Client.ToList();
    SelectClientcomboBox.DisplayMember = "ClientName";
    SelectClientcomboBox.ValueMember = "ClientID";
    SelectClientcomboBox.Invalidate();
}

当您想要访问此ID时,您可以获得所选的值。

private void SaveEstateButton_Click(object sender, EventArgs e)
{
    Estate estate = new Estate();
    estate.ClientID = SelectClientcomboBox.SelectedValue;
}