允许空值的唯一约束
Unique constraint allowing null values
是否可以在允许多个空值的模型中使用唯一的 属性。
实施正在使用 SQL 服务器,看起来这是可能的:SQL Server UNIQUE constraint with duplicate NULLs
有什么方法可以修改 CFE 中唯一性约束的行为吗?
感谢您的回答,
SQL 服务器生产者无法生成此特定的 SQL 语句。但是,您可以用过滤后的索引替换生成的索引。
如果您需要更改一个或两个索引,您可以创建一个名为 after_<default namespace>_tables.sql
的 sql 脚本。此脚本将由 SQL 生产者 (documentation) 自动执行。
-- TODO Drop unique index if exists
CREATE UNIQUE INDEX [IX_Cus_Cuo_Cus] ON [dbo].[Customer]([Customer_FullName])
WHERE [Customer_FullName] IS NOT NULL
如果需要更改很多索引,可以编写模板并使用SQL服务器模板生产者(documentation):
[%@ namespace name="CodeFluent.Model"%]
[%@ namespace name="CodeFluent.Model.Persistence"%]
/* [%=Producer.GetSignature()%] */
[%foreach(Table table in Producer.Project.Database.Tables) { if (table.Constraints.Count == 0) continue;%]
[%foreach(CodeFluent.Model.Persistence.Constraint constraint in table.Constraints) { if (constraint.ConstraintType != ConstraintType.Unique) continue; %]
-- TODO Drop unique index if exists
CREATE UNIQUE INDEX [[%=constraint.ShortName%]] ON [[%=CodeFluent.Producers.SqlServer.SqlServerProducer.GetOwner(table)%]].[%=table.FullName%] (
[%for(int i = 0; i < constraint.Columns.Count; i++) {%]
[%if(i != 0){%], [%}%][[%=constraint.Columns[i].Name%]]
[%}%]
)
WHERE [%for(int i = 0; i < constraint.Columns.Count; i++) {%]
[%if(i != 0){%]AND [%}%][[%=constraint.Columns[i].Name%]] IS NOT NULL
[%}%]
[%}%]
[%}%]
是否可以在允许多个空值的模型中使用唯一的 属性。 实施正在使用 SQL 服务器,看起来这是可能的:SQL Server UNIQUE constraint with duplicate NULLs
有什么方法可以修改 CFE 中唯一性约束的行为吗?
感谢您的回答,
SQL 服务器生产者无法生成此特定的 SQL 语句。但是,您可以用过滤后的索引替换生成的索引。
如果您需要更改一个或两个索引,您可以创建一个名为 after_<default namespace>_tables.sql
的 sql 脚本。此脚本将由 SQL 生产者 (documentation) 自动执行。
-- TODO Drop unique index if exists
CREATE UNIQUE INDEX [IX_Cus_Cuo_Cus] ON [dbo].[Customer]([Customer_FullName])
WHERE [Customer_FullName] IS NOT NULL
如果需要更改很多索引,可以编写模板并使用SQL服务器模板生产者(documentation):
[%@ namespace name="CodeFluent.Model"%]
[%@ namespace name="CodeFluent.Model.Persistence"%]
/* [%=Producer.GetSignature()%] */
[%foreach(Table table in Producer.Project.Database.Tables) { if (table.Constraints.Count == 0) continue;%]
[%foreach(CodeFluent.Model.Persistence.Constraint constraint in table.Constraints) { if (constraint.ConstraintType != ConstraintType.Unique) continue; %]
-- TODO Drop unique index if exists
CREATE UNIQUE INDEX [[%=constraint.ShortName%]] ON [[%=CodeFluent.Producers.SqlServer.SqlServerProducer.GetOwner(table)%]].[%=table.FullName%] (
[%for(int i = 0; i < constraint.Columns.Count; i++) {%]
[%if(i != 0){%], [%}%][[%=constraint.Columns[i].Name%]]
[%}%]
)
WHERE [%for(int i = 0; i < constraint.Columns.Count; i++) {%]
[%if(i != 0){%]AND [%}%][[%=constraint.Columns[i].Name%]] IS NOT NULL
[%}%]
[%}%]
[%}%]