加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

.net – 将Linq中的外键设置为SQL

发布时间:2021-03-07 09:48:57 所属栏目:编程 来源:网络整理
导读:众所周知,假如已经加载了实体,则无法直接在Linq to SQL中配置外键ID.可是,您可以通过它的外键查找实体,然后行使实体相关将实体配置为外部实体. (我在这里取出了列举,并行使整数值来简化).即假如我有一个加载的约会实体和一个相干的AppoinmentStatus实体,我不

众所周知,假如已经加载了实体,则无法直接在Linq to SQL中配置外键ID.可是,您可以通过它的外键查找实体,然后行使实体相关将实体配置为外部实体. (我在这里取出了列举,并行使整数值来简化).即假如我有一个加载的约会实体和一个相干的AppoinmentStatus实体,我不能这样做: –

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

我乃至可以把它酿成一个扩展要领,就像这样.

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

我也可以将它放在Linq to SQL分部类中,就像这样.

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 to SQL Appointment Entity分部类中,如下所示: –

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

我应该做什么,为什么,照旧有更好的选择?

办理要领

这有两个首要的头脑门户:

>将逻辑放在DataContext中(假如您手动编写DataContext,则为部门类或现实类).这背后的根基道理是你的DataContext已经知道你全部差异的实体,以是这不会发生任何特另外耦合,也不会导致类膨胀.

虽然,弱点是,假如你有几百个这样的API要领(最终你也许会),那么你的DataContext将很快酿成泥球,布满了任何措施员抉择抛出的随机查询API您可以实行通过将相干函数疏散到统一部门DataContext类的差异实例来整理它,但这现实上只是一个美化改造.
>将逻辑放在存储库类中,即AppointmentRepository.这种要领的两个利益是(a)在存储库和IoC框架上行使依靠注入的手段,以防您抉择变动数据模子,以及(b)您僵持行使Single Responsibility Principle – 它现实上是故意义的要领是它的位置.

将它们放入存储库的首要弱点是:(a)它们也许正在复制与DataContext中已存在的很是相似的逻辑作为存储进程; (b)他们在买卖营业打点方面有一种令人头疼的要领(假如你也用它们来生涯); (c)当你开始有大量的自界说查询返回特定操纵或陈诉的专门定制的DTO时,你可以选择为每个DTO建设一个存储库,可能建设一个主处事器.全部DTO的“适用措施”存储库或个中一些疏松相干的组.两者最终都是一个相等糟糕的计划.

这些是衡量;只有你可以抉择哪个更得当你本身的目标.

我必定会提议不要行使扩展要领,由于扩展要领很难发明(你不能只输入要领并让Intellisense选择相干参考),当你有手段时它们基础就没有须要直接修改或扩展(通过部门)原始类.

我还提议不要延迟预约课程;我们行使像Linq To SQL这样的器材的缘故起因之一是我们可以处理赏罚POCO实体,这些实体不必要知道它们来自那里.出于这个缘故起因,我小我私人很是阻挡将实体类与它们的DataContext耦合 – 依靠应该只是单向的.

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读