Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据库体系结构问题_C#_Sql_Linq To Sql - Fatal编程技术网

C# 数据库体系结构问题

C# 数据库体系结构问题,c#,sql,linq-to-sql,C#,Sql,Linq To Sql,我有一个SQL数据库,一旦建立,我将通过LINQ与之交互。 它有一个客户端,该客户端在两个表中有两个fkID字段(忽略其他字段) 类别国家 我试图做的是存储一个“规则”表,其中发生以下情况: if the Class is 'A' and the Country is 'USA' then create rows 'RowsA' in table todo else if the class is 'A' and the country is not 'USA' then

我有一个SQL数据库,一旦建立,我将通过LINQ与之交互。 它有一个客户端,该客户端在两个表中有两个fkID字段(忽略其他字段)

类别
国家

我试图做的是存储一个“规则”表,其中发生以下情况:

if the Class is 'A' and the Country is 'USA' then 
    create rows  'RowsA' in table todo

else if the class is 'A' and the country is not 'USA' then
    create rows 'RowsB'  in table todo

else
    create rows 'RowsC' in table todo


因此,在规则表中有这样做的最佳方法

规则ID标识
ClassFK Int null
CountryFK Int null
顺序 int不为空


并让我的C#代码执行以下操作:

var rules = from r in db.Rules
            orderby r.order
            select r;

foreach(rule in rules)
{
     if (((client.ClassFK == rule.ClassFK) || (rule.ClassFK == null)) &&             
           ((client.CountryFK== rule.CountryFK) || (rule.CountryFK== null)))
     {
          // Create The Rows
     }
}

这对我来说似乎非常脆弱

让它看起来更干净的一种方法是在规则类上创建一个方法,检查它是否适用于客户机;这使您的代码看起来像:

foreach(rule in rules.Where(r => r.AppliesTo(client)))
{
    // Create the Rows
}

但是,我看到缺少的部分是知道根据特定规则创建哪些行;这不包含在规则表中。

使用触发器如何?优点/缺点:对应用程序代码不太透明。我建议不要使用触发器。我认为我还没有找到使用触发器的有效理由,除非你想让下一个试图调试你的代码的程序员感到困惑。凯尔西,如果你的开发人员知道他们在做什么,那么有很多使用触发器的好理由。复杂的数据完整性规则必须在触发器中始终存在。在应用程序中处理它们,因为开发人员不了解数据库开发,这充其量是不负责任的(数据进入数据库的方式还有其他,而不是应用程序!),最坏的情况是对公司的底线有害。数据完整性应该胜过开发人员的无能。