C# System.Data.DataColumn中对nullable的唯一约束
在我的数据库中有这样一个表C# System.Data.DataColumn中对nullable的唯一约束,c#,asp.net,.net,database,unique-constraint,C#,Asp.net,.net,Database,Unique Constraint,在我的数据库中有这样一个表 table foo int pk int someFK NULL 在someFK上具有外键约束,在someFK上具有唯一约束。 这意味着在MySQL数据库中,除非我在someFK中指定NULL,否则在相应的表中当然必须有一行。但是,即使启用了唯一约束,我也可以在someFK中有几个空行 在我的代码中,我使用System.Data命名空间并执行以下操作: DataTable table = new DataTable("Foo"); DataColumn col =
table foo
int pk
int someFK NULL
在someFK上具有外键约束,在someFK上具有唯一约束。
这意味着在MySQL数据库中,除非我在someFK中指定NULL,否则在相应的表中当然必须有一行。但是,即使启用了唯一约束,我也可以在someFK中有几个空行
在我的代码中,我使用System.Data命名空间并执行以下操作:
DataTable table = new DataTable("Foo");
DataColumn col = null;
DataColumn[] primaryKey = new DataColumn[1];
col = table.Columns.Add(FooPropertyName, typeof(int));
col.Unique = true;
col.AutoIncrement = true;
primaryKey[0] = col;
table.PrimaryKey = primaryKey;
col = table.Columns.Add(SomeFkPropertyName, typeof(int));
col.Unique = true;
col.AutoIncrement = false;
但是,如果我将两个DataRows添加到我的DataTable中,并且这两个数据行具有不同的主键,但在someFK列上都具有DBNull,则会收到一条错误消息
异常类型:System.Data.ConstraintException
异常消息:“somefk”列被约束为唯一。值“”已存在
这不是我所期望的,所以我想知道是否有人知道如何绕过这个问题(不删除唯一属性)您需要告诉DataTable null值被接受
col = table.Columns.Add(SomeFkPropertyName, typeof(int));
col.Unique = true;
col.AutoIncrement = false;
col.AllowDBNull = true;
更多
编辑1
你说得对,还是坏了
var table = new DataTable("Foo");
table.Columns.AddRange(new []
{
new DataColumn("FooPropertyName", typeof(int))
{
Unique = true,
AutoIncrement = true
},
new DataColumn("SomeFkPropertyName")
{
Unique = true,
AllowDBNull = true
},
});
table.PrimaryKey = new[] {table.Columns[0]};
table.Rows.Add(0, 0);
table.Rows.Add(1, 1);
table.Rows.Add(2, DBNull.Value);
table.Rows.Add(3, DBNull.Value); // Exception here
编辑2
这也不起作用:/
private class MyDbNull
{
public static MyDbNull Value = new MyDbNull();
public override bool Equals(object obj)
{
return false;
}
public override int GetHashCode()
{
return 0;
}
}
table.Rows.Add(2, MyDbNull.Value);
table.Rows.Add(3, MyDbNull.Value);
您需要告诉DataTable null值被接受
col = table.Columns.Add(SomeFkPropertyName, typeof(int));
col.Unique = true;
col.AutoIncrement = false;
col.AllowDBNull = true;
更多
编辑1
你说得对,还是坏了
var table = new DataTable("Foo");
table.Columns.AddRange(new []
{
new DataColumn("FooPropertyName", typeof(int))
{
Unique = true,
AutoIncrement = true
},
new DataColumn("SomeFkPropertyName")
{
Unique = true,
AllowDBNull = true
},
});
table.PrimaryKey = new[] {table.Columns[0]};
table.Rows.Add(0, 0);
table.Rows.Add(1, 1);
table.Rows.Add(2, DBNull.Value);
table.Rows.Add(3, DBNull.Value); // Exception here
编辑2
这也不起作用:/
private class MyDbNull
{
public static MyDbNull Value = new MyDbNull();
public override bool Equals(object obj)
{
return false;
}
public override int GetHashCode()
{
return 0;
}
}
table.Rows.Add(2, MyDbNull.Value);
table.Rows.Add(3, MyDbNull.Value);
不,默认值是真的(请参见您自己的链接)查看您的编辑:无论如何,类似乎缺少外键列上的某种唯一性概念(建模(0-1)-(1)关系ship我将把您的答案设置为接受,因为我认为我们同意这不符合我的观点。不幸的是,不,默认值是真的(查看您自己的链接)查看您的编辑:无论如何,这些类似乎在外键列上缺少某种独特性的概念(建模一个(0-1)-(1)关系ship我会将您的答案设置为接受,因为我认为我认为这不符合我的意见。不幸的是,与论坛网站不同,我们不使用“谢谢”或“感谢”与论坛网站不同,我们不使用“谢谢”或“感谢任何帮助”,也不使用.See上的签名。