为没有 UNIQUE 约束的列生成唯一的 number/value
Generate a unique number/value for a column that does not have UNIQUE constraint
我有这个实体(在数据库和 Entity Framework 中):
public class LanguageValue
{
public int ID { get; set; }
public long Key { get; set; } // bigint in database
public int LanguageID { get; set; } // FK with a Language table in database
public string Value { get; set; } // nvarchar(MAX) in database
public bool Active { get; set; } // bit in database
}
我已经读过这篇文章,但我的问题有点不同:EF6: How to generate a unique number automatically based on entity Id
我的 Key
列没有唯一约束,因为可以有多个行具有相同的 Key
值。例如:
ID Key LanguageID Value
---------------------------------------------------
1 1 1 Name in Language 1
2 1 2 Name in Language 2
我不能使用映射 table 代替 Key
来执行 FK,因为多个 table 使用相同的 table(产品名称、类别名称、文章的内容).
现在每当我需要插入 Product
或 Category
记录时,我需要插入它们相应的语言值。但是,我还没有找到一种方法来生成其中唯一的 Key,而且需要唯一,以防同时处理两个插入(例如,同时添加 Product 和 Category) .
我正在考虑 Guid
,这应该是我对 RNG 的安全赌注,还是有更好的 way/design 来实现它?
好的,我认为我的问题是数据库设计不当造成的。我把LanguageValue
table分成两个table,如下图,实现了之前设计没有的2个优点:
- 我现在可以安全地插入
LanguageKey
和 IDENTITY
。
- 我现在可以拥有产品和类别
NameLanguageKey
的 FK,这在以前是不可能的,因为 Key
不是唯一的。
我有这个实体(在数据库和 Entity Framework 中):
public class LanguageValue
{
public int ID { get; set; }
public long Key { get; set; } // bigint in database
public int LanguageID { get; set; } // FK with a Language table in database
public string Value { get; set; } // nvarchar(MAX) in database
public bool Active { get; set; } // bit in database
}
我已经读过这篇文章,但我的问题有点不同:EF6: How to generate a unique number automatically based on entity Id
我的 Key
列没有唯一约束,因为可以有多个行具有相同的 Key
值。例如:
ID Key LanguageID Value
---------------------------------------------------
1 1 1 Name in Language 1
2 1 2 Name in Language 2
我不能使用映射 table 代替 Key
来执行 FK,因为多个 table 使用相同的 table(产品名称、类别名称、文章的内容).
现在每当我需要插入 Product
或 Category
记录时,我需要插入它们相应的语言值。但是,我还没有找到一种方法来生成其中唯一的 Key,而且需要唯一,以防同时处理两个插入(例如,同时添加 Product 和 Category) .
我正在考虑 Guid
,这应该是我对 RNG 的安全赌注,还是有更好的 way/design 来实现它?
好的,我认为我的问题是数据库设计不当造成的。我把LanguageValue
table分成两个table,如下图,实现了之前设计没有的2个优点:
- 我现在可以安全地插入
LanguageKey
和IDENTITY
。 - 我现在可以拥有产品和类别
NameLanguageKey
的 FK,这在以前是不可能的,因为Key
不是唯一的。