.net 将Linq中的外键设置为SQL
众所周知,如果实体已经加载,则不能在Linq中将外键ID直接设置为SQL。但是,您可以通过实体的外键查找该实体,然后使用实体关系将该实体设置为外部实体。(为了简单起见,我在这里取出了枚举并使用了整数值)。i、 e.如果我有一个加载的约会实体和一个关联的约会状态实体,我不能这样做:-.net 将Linq中的外键设置为SQL,.net,vb.net,linq-to-sql,.net,Vb.net,Linq To Sql,众所周知,如果实体已经加载,则不能在Linq中将外键ID直接设置为SQL。但是,您可以通过实体的外键查找该实体,然后使用实体关系将该实体设置为外部实体。(为了简单起见,我在这里取出了枚举并使用了整数值)。i、 e.如果我有一个加载的约会实体和一个关联的约会状态实体,我不能这样做:- ExistingAppointment.AppointmentStatusID = 7 ExistingAppointment.AppointmentStatus = (From appstat In db.App
ExistingAppointment.AppointmentStatusID = 7
ExistingAppointment.AppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.StatusID = 7 _
Select appstat).Single
但我可以做到:-
ExistingAppointment.AppointmentStatusID = 7
ExistingAppointment.AppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.StatusID = 7 _
Select appstat).Single
我的代码中到处都是这种东西,我想进行重构。所以
我显然可以在这样的模块中使用助手方法:-
Module Helper
Public Shared Function GetAppointmentStatus(ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatus _
Select appstat).Single
End Function
End Module
Partial Public Class Appointment
Public Function GetAppointmentStatus(ByVal db as DataClassesDataContext, ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatusID _
Select appstat).Single
End Function
End Class
我甚至可以把它变成一个扩展方法,就像这样
Imports System.Runtime.CompilerServices
Module Helper
Extension()> _
Public Shared Function GetAppointmentStatus(ByVal db as DataClassesDataContext, ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatusID _
Select appstat).Single
End Function
End Module
Partial Public Class DataClassesDataContext
Public Function GetAppointmentStatus(ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In Me.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatusID _
Select appstat).Single
End Function
End Class
我还可以把它放在LINQtoSQL部分类中,如下所示
Imports System.Runtime.CompilerServices
Module Helper
Extension()> _
Public Shared Function GetAppointmentStatus(ByVal db as DataClassesDataContext, ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatusID _
Select appstat).Single
End Function
End Module
Partial Public Class DataClassesDataContext
Public Function GetAppointmentStatus(ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In Me.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatusID _
Select appstat).Single
End Function
End Class
此外,我可以将代码放在Linq到SQL约会实体部分类中,如下所示:-
Module Helper
Public Shared Function GetAppointmentStatus(ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatus _
Select appstat).Single
End Function
End Module
Partial Public Class Appointment
Public Function GetAppointmentStatus(ByVal db as DataClassesDataContext, ByVal AppStatusID As Integer) As AppointmentStatus
GetAppointmentStatus = (From appstat In db.AppointmentStatus _
Where appstat.AppointmentStatusID = AppStatusID _
Select appstat).Single
End Function
End Class
我应该做什么以及为什么,或者有更好的替代方案吗?通常,AppointmentStatusId的设置器看起来像:(用C#编写代码,但应该很容易获得)
我通常将这些方法放在DataContext的分部类中,理论上这些方法在某种意义上类似于存储过程,并且存储过程在DataContext上表现为方法
无论您决定以哪种方式继续,都绝对值得重构,以便您只在一个地方使用此查询,而不会重复您自己的操作。这也让您可以选择将您描述的简单方法替换为更复杂的方法,该方法可以缓存查询的编译版本并重用它,而无需更新该方法的所有调用者。关于这一点,有两个主要的思想流派:
DataContext
(部分类,如果手动编写DataContext
,则为实际类)。这背后的基本原理是,您的DataContext
已经知道所有不同的实体,因此这不会产生任何额外的耦合,也不会导致类膨胀
当然,缺点是,如果你有几百种这样的API方法(你可能最终会有),那么你的DataContext
将很快变成一团泥,填满了程序员决定扔进的每一个随机查询API。您可以尝试通过将相关函数分离到同一部分DataContext
类的不同实例中来解决这个问题,但这实际上只是一个表面上的改进
AppointmentRepository
。这种方法的两个优点是(a)在您决定更改数据模型的情况下,能够在存储库和IoC框架上使用依赖项注入,以及(b)您坚持的事实-方法的位置实际上是合理的
将它们放在存储库中的主要缺点是:(a)它们可能会复制与存储过程非常相似的逻辑,这些逻辑已经存在于DataContext
中;(b) 当涉及到事务管理时(如果您也使用它们来保存数据),它们会带来麻烦;(c)当您开始有许多定制查询返回针对特定操作或报告的专门定制的DTO时,您将面临两个糟糕的选择:要么为每个DTO创建一个存储库,要么为所有DTO或其中一些松散相关的组创建一个主“实用工具”存储库。两者最终都是一个相当糟糕的设计
我还建议不要延长
预约
课程;我们使用linqtosql这样的工具的原因之一是,我们可以处理那些不需要知道它们来自何处的POCO实体。出于这个原因,我个人非常反对将实体类耦合到它们的DataContext
——依赖关系应该是单向的。“众所周知,不能在Linq中直接设置外键ID到SQL”-这对我来说是新闻。我一直都在这样做。如果实体已经加载了呢??