我们的 SQL 或实体框架中的错误?

Posted

技术标签:

【中文标题】我们的 SQL 或实体框架中的错误?【英文标题】:Bug in our SQL or Entity Framework? 【发布时间】:2010-10-14 12:06:54 【问题描述】:

我有一些来自 DBA 的 SQL,我只是在检查它是否正确,因为 EF 似乎没有将实体完全链接在一起。它知道存在关系,但不执行 FK 到 PK 链接。

任何想法或想法(而不是使用 NHibernate!)都表示赞赏。

1 创建表 [dbo].[雇员]( 2 [ID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, 3 [PersonID] [int] 非空, 4 [性别ID] [int] NULL, 5 [EthnicOriginID] [int] NULL, 6 [婚姻状态ID] [int] NULL, 7 [出生日期] [日期时间] NULL, 8 [离开日期] [日期时间] NULL, 9 [OptOut] [bit] NULL CONSTRAINT [DF_employee_OptOut] DEFAULT (0), 10 [OptOutDate] [日期时间] NULL, 11 [PassportNumber] [nvarchar](50) NULL, 12 [WorkPermitNumber] [nvarchar](50) 空值, 13 [WorkPermitExpiryDate] [日期时间] NULL, 14 [PayrollNumber] [nvarchar](50) NULL, 15 [NINumber] [nvarchar](50) 空值, 16 [Visa] [bit] NULL CONSTRAINT [DF_employee_Visa] DEFAULT (0), 17 [VisaNumber] [nvarchar](50) 空值, 18 [VisaExpiryDate] [smalldatetime] NULL, 19 [GuaranteeAmount] [money] NULL, 20 [GuaranteeDuration] [int] NULL, 21 [保证结束日期] [日期时间] NULL, 22 [GuaranteePeriod] [int] NULL, 23 [IsDisabled] [bit] NULL 约束 [DF_employee_IsDisabled] 默认 (0), 24 [DisabilityReferenceNumber] [nvarchar](20) NULL, 25 [IsActive] [bit] NULL 约束 [DF_employee_Active] 默认 (1), 26 [IsUKResident] [位] NULL, 27 [银行帐户] [nvarchar](100) NULL, 28 约束 [PK_employee] 主键集群 29 ( 30 [ID] ASC 31)与(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY], 32 约束 [IX_employee] 唯一非集群 33 ( 34 [ID] ASC 35)与(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [主要] 36) 开 [主要] 37 38 去 39 ALTER TABLE [dbo].[Employee] WITH NOCHECK ADD CONSTRAINT [FK_Employee_People] 外键([PersonID]) 40 参考文献 [dbo].[Person] ([Id]) 41 不可复制 42 去 43 ALTER TABLE [dbo].[Employee] NOCHECK 约束 [FK_Employee_People] 44 45 46 ---- 47 48 创建表 [dbo].[EmployeeWorkHistory]( 49 [id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, 50 [EmployeeId] [int] 非空, 51 [职位名称] [nvarchar](50) 空值, 52 [OfficeId] [int] NULL, 53 [部门ID] [int] NULL, 54 [开始日期] [小日期时间] NULL, 55 [结束日期] [小日期时间] NULL, 56 [工资] [十进制](18, 0) NULL, 57 [ContractTypeId] [int] NULL, 58 [评论ID] [int] NULL, 59 [工作时间] [浮点数] NULL, 60 [ManagerEmployeeID] [int] NULL, 61 [EmployeeTypeID] [int] NULL, 62 [LastWorkingDay] [smalldatetime] NULL, 63 [GardenLeave] [位] NULL 约束 [DF_employeeWorkHistory_GardenLeave] 默认 (0), 64 [WorkingHours] [nvarchar](20) 空约束 [DF_EmployeeWorkHistory_WorkingHours] 默认('8.30am - 5.30pm'), 65 [WorkingDays] [nvarchar](100) NULL CONSTRAINT [DF_EmployeeWorkHistory_WorkingDays] DEFAULT ('Monday to Friday'), 66 [TerminationId] [int] NULL, 67 [终止日期] [小日期时间] NULL, 68 约束 [PK_employeeWorkHistory] ​​主键集群 69 ( 70 [编号] ASC 71)与(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [主要] 72) 开 [主要] 73 74 去 75 EXEC sys.sp_addextendedproperty @name=N'MS_Description',@value=N'',@level0type=N'SCHEMA',@level0name=N'dbo',@level1type=N'TABLE',@level1name=N'EmployeeWorkHistory ', @level2type=N'COLUMN',@level2name=N'WorkingHours' 76 去 77 ALTER TABLE [dbo].[EmployeeWorkHistory] ​​WITH NOCHECK 添加约束 [FK_EmployeeWorkHistory_ContractType] 外键([ContractTypeId]) 78 参考 [dbo].[ContractType] ([Id]) 79 不可复制 80 去 81 ALTER TABLE [dbo].[EmployeeWorkHistory] ​​检查约束 [FK_EmployeeWorkHistory_ContractType] 82 去 83 ALTER TABLE [dbo].[EmployeeWorkHistory] ​​WITH NOCHECK 添加约束 [FK_EmployeeWorkHistory_Employee] 外键([EmployeeId]) 84 参考 [dbo].[雇员] ([ID]) 85 不可复制 86 去 87 ALTER TABLE [dbo].[EmployeeWorkHistory] ​​检查约束 [FK_EmployeeWorkHistory_Employee] 88 去 89 90 91 ------ 92 93 94 创建表[dbo].[合同类型]( 95 [Id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, 96 [文本] [nvarchar](50) 非空, 97 [IsActive] [bit] 非空约束 [DF_ContractType_IsActive] 默认 (1), 98 约束 [PK_ContractType] 主键集群 99 ( 100 [编号] ASC 101)与(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON,FILLFACTOR = 100)ON [主要] 102) 开 [主要] 103 104 -- 105 106 创建表 [dbo].[EmployeeReference]( 107 [ID] [int] IDENTITY(1,1) 非空, 108 [EmployeeID] [int] 非空, 109 [RefereePersonID] [int] 非空, 110 [公司] [nvarchar](200) 空值, 111 [评论ID] [int] NULL, 112 [日期请求] [小日期时间] NULL, 113 [日期接收] [小日期时间] NULL, 114 [TimeKnownFor] [nvarchar](100) 空值, 115 [参考文档] [nvarchar](500)NULL, 116 [ReferenceTypeID] [int] 非空, 117 [文件附件ID] [int] NULL, 118 [ContactDetailId] [int] NULL, 119 约束 [PK_EmployeeReference] 主键集群 120 ( 121 [ID] ASC 122)与(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [主要] 123)开[主要] 124 125 去 126 ALTER TABLE [dbo].[EmployeeReference] WITH NOCHECK 添加约束 [FK_EmployeeReference_Person] 外键([RefereePersonID]) 127 参考文献 [dbo].[Person] ([Id]) 128 不可复制 129 去 130 更改表 [dbo].[EmployeeReference] NOCHECK 约束 [FK_EmployeeReference_Person] 131 去 132 ALTER TABLE [dbo].[EmployeeReference] WITH NOCHECK 添加约束 [FK_EmployeeReferenceMapping_Employee] 外键([EmployeeID]) 133 参考 [dbo].[雇员] ([ID]) 134 不可复制 135 去 136 ALTER TABLE [dbo].[EmployeeReference] NOCHECK 约束 [FK_EmployeeReferenceMapping_Employee] 137 138—— 139 140 141创建表[dbo]。[人]( 142 [Id] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL, 第143章 144 [名字] [nvarchar](50)空, 145 [中间名] [nvarchar](50) NULL, 146 [姓氏] [nvarchar](50)NULL, 147 [首选名称] [nvarchar](50)NULL, 148 [ContactDetailId] [int] NULL, 第149章 150 [IsActive] [位] 非空约束 [DF_people_Active] 默认 (1), 151 [EnteredBy] [int] NULL, 152 [输入日期] [日期时间] NULL, 153 [更新者] [int] NULL, 154 [更新日期] [日期时间] NULL, 155约束[PK_person]主键集群 156 ( 第157章 158)WITH(PAD_INDEX = OFF,STATISTICS_NORECOMPUTE = OFF,IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON,ALLOW_PAGE_LOCKS = ON)ON [PRIMARY] 第159章 160 161 去 第162章 163

【问题讨论】:

为什么Employee上有重复的索引? 【参考方案1】:

你已经在外键约束上定义了 NOCHECK

  ALTER TABLE [dbo].[Employee] NOCHECK CONSTRAINT [FK_Employee_People] 

另外,您在 Employee 上有一个重复的索引。

【讨论】:

以上是关于我们的 SQL 或实体框架中的错误?的主要内容,如果未能解决你的问题,请参考以下文章

在 Visual Studio 中使用实体框架与 SQL Server 的连接问题

将数据插入数据库时​​实体框架中的验证错误

带有 Firebird 的实体框架引发动态 SQL 错误

使用 SQL Server 数据库和实体框架错误的 Windows 应用程序部署

实体框架中的连接泄漏

在 Toastr 或使用 MVC4 和实体框架的类似方法中显示错误和异常