Sql server 由于连接,LINQ到SQL插入失败
我的数据库中有两个表Sql server 由于连接,LINQ到SQL插入失败,sql-server,linq-to-sql,join,Sql Server,Linq To Sql,Join,我的数据库中有两个表Users和HealthMonitor。在HealthMonitor表中,我有一个UserID字段,该字段映射到Users表中的ID字段 到目前为止相当直接 我将HealthMonitor表中的UserID字段保留为“Nullable”,这样,如果当前没有用户登录,我就可以让系统在表中插入NULL值。。。IE:如果错误被抛出到未经身份验证的用户 问题是,当我试图在UserID字段中插入NULL时,插入失败 仅供参考:消除关系也会消除问题,但我知道这可能不是完成任务的最佳方式
Users
和HealthMonitor
。在HealthMonitor
表中,我有一个UserID
字段,该字段映射到Users
表中的ID
字段
到目前为止相当直接
我将HealthMonitor
表中的UserID
字段保留为“Nullable”,这样,如果当前没有用户登录,我就可以让系统在表中插入NULL值。。。IE:如果错误被抛出到未经身份验证的用户
问题是,当我试图在UserID
字段中插入NULL时,插入失败
仅供参考:消除关系也会消除问题,但我知道这可能不是完成任务的最佳方式
编辑:
在重新应用关系并确保UserID
可为空后,它现在似乎可以工作了。。。但是,插入的值为0
notnull
Public Sub AddException(ByVal ex As Exception, Optional ByVal notes As String = Nothing) Implements IHealthMonitorService.AddException
Dim exception As HealthMonitor = New HealthMonitor
Dim userID As Integer = Nothing
If HttpContext.Current.User.Identity.IsAuthenticated Then userID = Authentication.CustomAuthentication.RetrieveAuthUser.ID
Dim DataConverter As Utilities.DataConverters = New Utilities.DataConverters
Dim InformationHelper As Utilities.InformationHelper = New Utilities.InformationHelper
With exception
.DateTime = DateTime.Now
.Exception = ex.ToString
.Message = ex.Message
.Notes = notes
.ShortMessage = If(ex.Message.Length > 50, ex.Message.Substring(0, 50), ex.Message)
.Source = ex.Source
.StackTrace = ex.StackTrace
.Url = HttpContext.Current.Request.Url.ToString()
.UserID = userID
.UserIP = DataConverter.IPAddressToNumber(InformationHelper.GetUserIP)
.UserOS = InformationHelper.GetUserOS()
.UserBrowser = InformationHelper.GetUserBrowser()
End With
_HealthMonitorRepository.AddException(exception)
End Sub
我是不是遗漏了什么?是否可以将其插入为NULL
编辑:
好吧,我撒了一点谎。我仍然得到错误
Public Sub AddException(ByVal exception As HealthMonitor) Implements IHealthMonitorRepository.AddException
dc.HealthMonitors.InsertOnSubmit(exception)
dc.SubmitChanges() ''# ERROR HERE
End Sub
INSERT语句与外键约束“FK_un_HealthMonitor_un_Users”冲突。冲突发生在数据库“UrbanNow”、表“dbo.un_Users”、列“ID”中。
声明已终止
这是桌子
/****** Object: Table [dbo].[un_HealthMonitor] Script Date: 07/24/2010 10:48:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[un_HealthMonitor](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NULL,
[Exception] [nvarchar](4000) NULL,
[Url] [nvarchar](2048) NULL,
[Source] [nvarchar](4000) NULL,
[ShortMessage] [nvarchar](50) NULL,
[Message] [nvarchar](4000) NULL,
[StackTrace] [nvarchar](4000) NULL,
[UserIP] [nvarchar](50) NULL,
[UserBrowser] [nvarchar](50) NULL,
[UserOS] [nvarchar](50) NULL,
[UserIsMoblie] [bit] NULL,
[DateTime] [datetime2](0) NULL,
[Notes] [nvarchar](4000) NULL,
CONSTRAINT [PK_un_HealthMonitor] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[un_HealthMonitor] WITH CHECK ADD CONSTRAINT [FK_un_HealthMonitor_un_Users] FOREIGN KEY([UserID])
REFERENCES [dbo].[un_Users] ([ID])
GO
ALTER TABLE [dbo].[un_HealthMonitor] CHECK CONSTRAINT [FK_un_HealthMonitor_un_Users]
GO
搞定了
所以基本上在我的服务层我有
Public Sub AddException(ByVal ex As Exception, Optional ByVal notes As String = Nothing) Implements IHealthMonitorService.AddException
Dim exception As HealthMonitor = New HealthMonitor
Dim userID As Integer = Nothing
If HttpContext.Current.User.Identity.IsAuthenticated Then userID = Authentication.CustomAuthentication.RetrieveAuthUser.ID
Dim DataConverter As Utilities.DataConverters = New Utilities.DataConverters
Dim InformationHelper As Utilities.InformationHelper = New Utilities.InformationHelper
With exception
.DateTime = DateTime.Now
.Exception = ex.ToString
.Message = ex.Message
.Notes = notes
.ShortMessage = If(ex.Message.Length > 50, ex.Message.Substring(0, 50), ex.Message)
.Source = ex.Source
.StackTrace = ex.StackTrace
.Url = HttpContext.Current.Request.Url.ToString()
.UserID = userID
.UserIP = DataConverter.IPAddressToNumber(InformationHelper.GetUserIP)
.UserOS = InformationHelper.GetUserOS()
.UserBrowser = InformationHelper.GetUserBrowser()
End With
_HealthMonitorRepository.AddException(exception)
End Sub
问题是,当我将userID
发送到异常对象时,它将0
(零)发送到数据库。这是解决办法
Dim userID As Integer? = Nothing ''# (?) makes the integer nullable.
为了确保您有一个用户,因此不会违反外键,您可以执行以下操作:
`Public Sub AddException(ByVal exception As HealthMonitor) Implements IHealthMonitorRepository.AddException
' take what you need from exception to Create a User
User user = new User{Populate properties here}
dc.Users.InsertOnSubmit(user)
dc.HealthMonitors.InsertOnSubmit(exception)
dc.SubmitChanges() ''# ERROR HERE
End Sub'
这样你就可以在需要的时候创建用户
或者您可以为用户执行存在性检查。与其让字段为空,不如用一个触发器来填充一个已知值(“N/a”或“0”或类似值)。触发器-真恶心。如果是这种情况,我可以通过LINQ提交一个“0”值,但仍然失败,因为用户中没有相关的“0”记录table@JNK,当关系的另一端没有任何内容时,Null是正确的值。采取这样的变通办法将是一种耻辱@RockingTheSixstring,您使用的是VS吗?是否已在.dbml设计器中签入该属性标记为Nullable?可能是导入不正确。我没有在设计器中进行检查。我马上去看看。好像当我重建它并检查以确保UserID可以为空时,它现在可以工作了。我编辑了我的问题,因为值是“0”而不是“NULL”,我不想为网站的匿名访问者创建用户,这太可笑了。我找到了答案,看一看。