获取Scope_identity

时间:2013-02-14 12:02:35

标签: c# linq linq-to-sql

我有一个新的插入,然后我需要获取刚刚插入的项目的ID

        var provider = new tbl_Provider
        {
            provider_Name = txt_ProviderName.Text,
            provider_Contact = txt_ProviderContactName.Text,
            provider_Address1 = txt_Address1.Text,
            provider_Address2 = txt_Address2.Text,
            provider_Address3 = txt_Address3.Text,
            provider_Postcode = txt_Postcode.Text,
            provider_Telephone = txt_Telephone.Text,
            provider_Deleted = false
        };

        //provider.tbl_ProviderDetails. = lst_ServiceAreas.SelectedValue;                       

        // Insert
        // ======
        _db.tbl_Providers.InsertOnSubmit(provider);

        // Save
        // ====
        _db.SubmitChanges();

我如何在Linq中这样做。

我之前选择了Scope Identity,但我在这个项目上尝试了Linq。

1 个答案:

答案 0 :(得分:1)

试试这种方式。更多info

        int Id = 0;
        provider = new tbl_Provider
        {
            provider_Name = txt_ProviderName.Text,
            ...
        };
        // Insert
        _db.tbl_Providers.InsertOnSubmit(provider);

        // Save
        _db.SubmitChanges();
        Id = provider.ProviderId;