[英]Entity Framework 6 tablename as FK value
重新設計您的數據庫,以在用戶定義的屬性持有者(例如學校)和屬性本身之間建立一個表:
CREATE TABLE Schools (
Id bigint IDENTITY(1,1) PRIMARY KEY NOT NULL,
Name nvarchar(50) NOT NULL,
AttributeOwnerId bigint -- This column should have both a FOREIGN KEY constraint on AttributeOwners.Id and a UNIQUE constraint (so no two schools can share the same user-defined attributes)
)
CREATE TABLE AttributeOwners (
Id bigint IDENTITY(1,1) PRIMARY KEY NOT NULL
)
CREATE TABLE Attributes (
Id bigint IDENTITY(1,1) PRIMARY KEY NOT NULL
AttributeOwnerId bigint -- FOREIGN KEY constraint on AttributeOwners.Id
Name nvarchar(50),
Value nvarchar(50)
)
通過這種設計,您現在可以擁有用戶定義字段的數據庫級參照完整性。 實體框架將其公開為每個 School 實體(或通過AttributeOwners
與Attributes
具有 FK 關系的任何其他實體)上的 Attributes 集合。
有一個小的設計錯誤,如果您有兩個表Schools
和Colleges
,它們都鏈接到AttributeOwners
那么它們都可以指向相同的AttributeOwners.Id
值,因此它們將共享用戶定義的屬性值。 您可以通過使用檢查其他表(通過 UDF)的CHECK CONSTRAINT
來緩解這種情況,但是只要將新表添加到您的設計中,就需要更新。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.