我已经建模了一个数据库(用于个人挑战的CMS),14个具有许多外键约束的表(特别是1 - > 0,1关系),我将用默认值填充表。
我面临的挑战是以什么顺序检测某种方法来填充表格,以避免插入时出错。
有没有一些不同的方法来做到这一点?由于序列错误,我在初始化脚本中不断出现错误!
简短说明:页面可以包含内容实体,但不一定。内容可以包含一个或多个部分,但不一定。页面和内容都可以作为独立存在。页面可以是meny等社区的一部分。
我正在使用实体框架作为我的orm而且我稍微修改了edmx,所以当我做一个>很多关系时,我没有得到参考,f.ex。
这是我的模特和关系:
这是我的初始化脚本,只是用户和其他一些表,只是为了开始使用登录程序的编码:
USE [WebMateCMS]
GO
INSERT INTO [Category]
VALUES ('93dad9c7-6b92-4f3b-a8a9-5a0c330290fc',
'Icon Flag',
'Icon Flags for the different languages',
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [Image]
VALUES ('2f426aa0-752b-4032-a6d0-83d84eb4d0e3',
'English-Flag',
'English Icon Flag',
null, --ImageTypeID
null, --IsThumbnailOfID
'System string filler', --RecordedBy
'System string filler', --Copyright
'System string filler', --Meta
'System string filler', --OriginalSizePx
'System string filler', --Base64Original
'System string filler', --BinaryOriginal
'System string filler', --FilePath
null, --CategoryID
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [Image]
VALUES ('57972deb-6a77-43f8-8b96-a6617a1c7efa',
'Danish-Flag',
'Danish Icon Flag',
null, --ImageTypeID
null, --IsThumbnailOfID
'System string filler', --RecordedBy
'System string filler', --Copyright
'System string filler', --Meta
'System string filler', --OriginalSizePx
'System string filler', --Base64Original
'System string filler', --BinaryOriginal
'System string filler', --FilePath
null, --CategoryID
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [Language]
VALUES ('2f426aa0-752b-4032-a6d0-83d84eb4d0e3',
'English',
'English language',
'en',
null,
'2016-03-17T08:32:55',
'2016-03-17T08:32:55',
'2016-03-17T08:32:55',
null,
null,
null,
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
null,
null,
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
null)
INSERT INTO [Language]
VALUES ('57972deb-6a77-43f8-8b96-a6617a1c7efa',
'Dansk',
'Dansk sprog',
'dk',
null,
'2016-03-17T08:32:55',
'2016-03-17T08:32:55',
'2016-03-17T08:32:55',
null,
null,
null,
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
null,
null,
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
null)
INSERT INTO [UsedPassword]
VALUES ('5b8c3e64-d069-41c6-9554-0cf9c7da9fb7',
'Used passwords for administrator@webmatecms.com',
'Used passwords for administrator@webmatecms.com',
null, --UsedPasswords
null, --UsedSalts
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [UsedPassword]
VALUES ('bffec845-a4fb-477a-bfcc-b7dbb97224cf',
'Used passwords for user@webmatecms.com',
'Used passwords for user@webmatecms.com',
null, --UsedPasswords
null, --UsedSalts
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [Credential]
VALUES ('5b8c3e64-d069-41c6-9554-0cf9c7da9fb7',
'administrator@webmatecms.com',
'Credential for administrator@webmatecms.com',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', --PassWord
'bbbbbbbbb', --Salt
null, --Token
null, --Certificate
null, --Company
null, --ShowClass
null, --RoleID
null, --UsedPassWordsID
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [Credential]
VALUES ('bffec845-a4fb-477a-bfcc-b7dbb97224cf',
'user@webmatecms.com',
'Credential for user@webmatecms.com',
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', --PassWord
'bbbbbbbbb', --Salt
null, --Token
null, --Certificate
null, --Company
null, --ShowClass
null, --RoleID
null, --UsedPassWordsID
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
INSERT INTO [User]
VALUES ('5b8c3e64-d069-41c6-9554-0cf9c7da9fb7',
'Administrator',
'User account for the administrator',
'User',
'RootUser',
'administrator@webmatecms.com',
null, --MobilePhoneNo
null, --ProfileImageID
null,
'RootUser',
'5b8c3e64-d069-41c6-9554-0cf9c7da9fb7',
null,
null,
null,
null,
null,
null,
null,
1,
1,
1,
'2f426aa0-752b-4032-a6d0-83d84eb4d0e3', -- languageid
0, --IsBlockedForSecurityReason
'2016-03-17T08:32:55',
'2016-03-17T08:32:55',
'2016-03-17T08:32:55',
null,
null,
null,
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
null,
null,
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- System User account
null)
INSERT INTO [User]
VALUES ('bffec845-a4fb-477a-bfcc-b7dbb97224cf', --ID
'User', --Name
'User account for the ordinary user', --Description
'User', --FirstName
'OrdinaryUser', --LastName
'user@webmatecms.com', --Email
null, --MobilePhoneNo
null, --ProfileImageID
null, --ProfileThumbnailImageID
'OrdinaryUser', --UserName
'bffec845-a4fb-477a-bfcc-b7dbb97224cf', --CredentialID
null, --LastLoginAttempt
null, --LastActive
null, --SequentialLoginAttempts
null, --TooManyFaultyLoginAttempts
null, --SessionID
null, --PreferredLoginMethod
null, --PreferredIpAdr
1, --CanComment
1, --ReceiveNewsMail
1, --ReceiveNewContentNotification
'57972deb-6a77-43f8-8b96-a6617a1c7efa', -- languageid
0, --IsBlockedForSecurityReason
'2016-03-17T08:32:55', --IsCreated
'2016-03-17T08:32:55', --IsValidFrom
'2016-03-17T08:32:55', --IsActivated
null, --IsDeleted
null, --IsEdited
null, --IsExpired
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- CreatedByNameID/System User account
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ValidFromByNameID/System User account
null, --EditedByNameID
null, --ExpiredByNameID
'65d6a29b-30b4-4d42-9b20-450182f393d2', -- ActivatedByNameID/System User account
null) --DeletedByNameID
此脚本会插入除最后两次插入(用户)之外的所有内容,因为它会生成错误:
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
Msg 547, Level 16, State 0, Line 210
The INSERT statement conflicted with the FOREIGN KEY constraint "User_ImageID_ZeroOrOne". The conflict occurred in database "WebMateCMS", table "dbo.Image", column 'ID'.
The statement has been terminated.
Msg 547, Level 16, State 0, Line 247
The INSERT statement conflicted with the FOREIGN KEY constraint "User_ImageID_ZeroOrOne". The conflict occurred in database "WebMateCMS", table "dbo.Image", column 'ID'.
The statement has been terminated.
这是我的外键约束;
--Adding Foreign Key constraints--
--Page--
ALTER TABLE Page
ADD CONSTRAINT Page_LanguageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Language](ID)
ALTER TABLE Page
ADD CONSTRAINT Page_ImageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Image](ID)
ALTER TABLE Page
ADD CONSTRAINT Page_MenuID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Menu](ID)
--Content--
ALTER TABLE [Content]
ADD CONSTRAINT Content_PageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Page](ID)
ALTER TABLE [Content]
ADD CONSTRAINT Content_DocumentTypeID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Type](ID)
ALTER TABLE [Content]
ADD CONSTRAINT Content_IsLockedByNameID_Many FOREIGN KEY (IsLockedByNameID) REFERENCES [User](ID)
ALTER TABLE [Content]
ADD CONSTRAINT Content_WrittenByNameID_Many FOREIGN KEY (WrittenByNameID) REFERENCES [User](ID)
ALTER TABLE [Content]
ADD CONSTRAINT Content_LanguageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Language](ID)
ALTER TABLE [Content]
ADD CONSTRAINT Content_ImageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Image](ID)
--Section--
ALTER TABLE [Section]
ADD CONSTRAINT Section_ContentID_Many FOREIGN KEY (ContentID) REFERENCES [Content](ID)
ALTER TABLE [Section]
ADD CONSTRAINT Section_ImageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Image](ID)
ALTER TABLE [Section]
ADD CONSTRAINT Section_IsLockedByNameID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [User](ID)
--Image--
ALTER TABLE [Image]
ADD CONSTRAINT Image_CategoryID_Many FOREIGN KEY (CategoryID) REFERENCES [Category](ID)
--Menu--
ALTER TABLE Menu
ADD CONSTRAINT Menu_LanguageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Language](ID)
--User--
ALTER TABLE [User]
ADD CONSTRAINT User_LanguageID_Many FOREIGN KEY (LanguageID) REFERENCES [Language](ID)
ALTER TABLE [User]
ADD CONSTRAINT User_ImageID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Image](ID)
ALTER TABLE [User]
ADD CONSTRAINT User_CredentialID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Credential](ID)
--Credentials--
ALTER TABLE [Credential]
ADD CONSTRAINT Credential_RoleID_Many FOREIGN KEY (RoleID) REFERENCES [Role](ID)
ALTER TABLE [Credential]
ADD CONSTRAINT Credential_UsedPasswordsID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [UsedPassword](ID)
--Role--
ALTER TABLE [Role]
ADD CONSTRAINT Role_PermissionID_Many FOREIGN KEY (ID) REFERENCES [Permission](ID)
--Language--
ALTER TABLE [Language]
ADD CONSTRAINT Language_IconID_ZeroOrOne FOREIGN KEY (ID) REFERENCES [Image](ID)
--Type--
ALTER TABLE Type
ADD CONSTRAINT Type_CategoryID_Many FOREIGN KEY (CategoryID) REFERENCES [Category](ID)
ALTER TABLE Type
ADD CONSTRAINT Type_TemplateID_Many FOREIGN KEY (TemplateID) REFERENCES [Template](ID)
答案 0 :(得分:0)
可视化有很大帮助。使用一些可以向你展示关系并可以遍历亲子关系的工具。
只需找到“最顶层”表(根),插入并转到它; s。孩子们。