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

本文关键字:lambda 表达式 检查 记录 删除 | 更新日期: 2023-09-27 18:28:17

我有两个表,一个叫Companies,另一个叫Locations。公司有Id和Name,地点有Id、CompanyId、Name和SubAccount。我有两个项目。一个是IMS。所有验证所在的数据和Web表单页面所在的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;
        }
    }

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

如果您有一个包含此数据和外键的关系数据库,并且设置正确,那么您可以提交更改并监视代码为547SqlException,这是一个外键异常。当要删除的数据被其他表引用时,会引发此问题。

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