在删除记录之前检查具有lambda表达式的外键

时间:2013-08-19 14:34:17

标签: c# lambda webforms

我有两个名为Companies的表,另一个名为Locations。公司有Id和Name,Locations有Id,CompanyId,Name和SubAccount。我有两个项目。一个是IMS.Data,其中所有验证都是和webforms页面所在的IMS。我无法验证如果公司有位置(如果公司ID是任何地方的外键),则不要删除记录。这是我到目前为止所有的工作,但我不能引用Locations CompanyId来使用lambda表达式进行检查。任何人都可以帮助我,我是lambda表达的新手。

以下是我用于验证的方法

    namespace IMS.Data
{
public class CompanyContext : IMSDBContext
{
    public Company DeleteCompany(Company company)
    {
        if (company.Name == null)
        {
            throw new Exception("Please select a record to delete.");
        }
        if (Companies.Any(x => x.Name == company.Name))
        {
            throw new Exception("Can not delete a company that has a location.");
        }
        Companies.Remove(company);
        return company;
    }

}
}

这是我使用的删除按钮

namespace IMS
{

public partial class CompanySetUp : Page
{
    private const string AddButton = "Add";
    private const string SaveButton = "Save";
    private const string DeleteButton = "Delete";
    private const string CancelButton = "Cancel";

    private int CompanyId // This puts the "CompanyId" into a viewstate and is used to update the record
    {
        get
        {
            return (int)ViewState["_companyId"];
        }
        set
        {
            ViewState["_companyId"] = value;
        }
    }

    private IList<Company> Companies { get; set; } // This gets and sets the list of companies from the table "Companies"

    protected void Page_Load(object sender, EventArgs e)
    {
        PopulateCompanyListGrid();
        //if (Companies != null && Companies.Count > 0) // This will put a record in the "txtCompanyName.Text" on page load
        //{
        //    txtCompanyName.Text = Companies.First().Name;
        //}

    }       

    protected void btnDelete_Click(object sender, EventArgs e) // This will delete the record that matches the textbox or throw an exception
    {
        CompanyContext context = null;
        switch (btnDelete.Text)
        {
            case DeleteButton:
                try
                {
                    context = new CompanyContext();
                    var company = context.Companies.ToList().First(x => x.Name == txtCompanyName.Text);

                    context.DeleteCompany(company);
                    //PopulateCompanyListGrid();
                    Reload();

                }
                catch (Exception ex)
                {
                    lblCompanyNameNotification.Text = ex.Message;
                }
                finally
                {
                    if (context != null)
                    {
                        context.Dispose();
                    }
                }
                PopulateCompanyListGrid();
                break;
            case CancelButton:
                Reload();
                break;
        }
    }

1 个答案:

答案 0 :(得分:1)

如果您拥有一个正确设置了此数据和外键的关系数据库,您可以提交更改并注意代码为SqlException的{​​{1}},这是一个外键异常。当要删除的数据被其他表引用时抛出此内容。

以这种方式处理它的优点是数据存储强制自身有效,而不是定义对代码中所有外键关系的检查。如果稍后添加新的FK,它们将自动由数据库强制执行并被代码捕获,而不是必须向代码本身添加新的检查。