实体框架参数化/重载构造函数?

发布于 2024-10-23 16:00:48 字数 1274 浏览 2 评论 0原文

我见过类似的问题,但它们并不完全是我所指的(或者也许它们是,但我不明白答案)

在我之前使用 Linq2SQL 的应用程序中,我能够通过这样做来使用参数重载构造函数:

Namespace CoreDb    
  Partial Public Class Accomplishment

    Public Sub New(ByVal accomplishmentTypeID As Object, ByVal description As String, ByVal title As String, ByVal applicableDate As DateTime, ByVal lastUpdatedBy As String)
        Me.New()

        If TypeOf (accomplishmentTypeID) Is Guid Then
            Me.AccomplishmentTypeId = accomplishmentTypeID
        End If

        If TypeOf (accomplishmentTypeID) Is String Then
            Me.AccomplishmentTypeId = New Guid(accomplishmentTypeID.ToString())
        End If

        Me.Description = description
        Me.ApplicableDate = applicableDate
        Me.Title = title
        Me.Id = Guid.NewGuid()
        Me.DateCreated = DateTime.Now
        Me.DateModified = DateTime.Now
        Me.LastUpdatedBy = lastUpdatedBy
        Me.CreatedBy = lastUpdatedBy
    End Sub
 End Class
End Namespace

基本上使用对象的分部类,共享 .dbml 文件的命名空间并调用默认构造函数,然后执行其他操作。

因此,在我的代码中,我可以执行以下操作:

Dim accomplishment As New Accomplishment(id, description, title, applicableDate, lastUpdatedBy)

这似乎不再在实体框架中工作,因为没有可调用的默认构造函数。

这不再起作用了吗?如果是这样的话,有什么好的替代方案来实现这样的事情呢?

I've seen similar questions but they aren't quite what I am referring to (or maybe they are and I don't understand the answers)

In my previous application using Linq2SQL I was able to overload constructors with parameters by doing this:

Namespace CoreDb    
  Partial Public Class Accomplishment

    Public Sub New(ByVal accomplishmentTypeID As Object, ByVal description As String, ByVal title As String, ByVal applicableDate As DateTime, ByVal lastUpdatedBy As String)
        Me.New()

        If TypeOf (accomplishmentTypeID) Is Guid Then
            Me.AccomplishmentTypeId = accomplishmentTypeID
        End If

        If TypeOf (accomplishmentTypeID) Is String Then
            Me.AccomplishmentTypeId = New Guid(accomplishmentTypeID.ToString())
        End If

        Me.Description = description
        Me.ApplicableDate = applicableDate
        Me.Title = title
        Me.Id = Guid.NewGuid()
        Me.DateCreated = DateTime.Now
        Me.DateModified = DateTime.Now
        Me.LastUpdatedBy = lastUpdatedBy
        Me.CreatedBy = lastUpdatedBy
    End Sub
 End Class
End Namespace

Basically using a partial class off the object, sharing the namespace of the .dbml file and calling the default constructor and then doing additional stuff.

So then in my code I could do something like:

Dim accomplishment As New Accomplishment(id, description, title, applicableDate, lastUpdatedBy)

This seems to no longer work in Entity Framework as there is no default constructor to call.

Does this no longer work? And if so what is a good alternative to implementing something like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一枫情书 2024-10-30 16:00:48

没有生成任何默认构造函数,但这并不意味着它没有。但是,当您在没有显式设置无参数构造函数的类中定义带参数的构造函数时,它将隐藏默认构造函数。

因此,在您的分部类中,您只需要定义一个无参数构造函数。

There is not any default constructor generated, but that doesn't mean it has none. But when you define a constructor with parameters in a class that has no parameterless constructor explicitly set, it will hide the default one.

So in you partial class you just need to also define a parameterless constructor.

执妄 2024-10-30 16:00:48

在撰写本文时的 EF 当前版本中,虽然您确实需要有一个无参数构造函数,但它似乎与 private 无参数构造函数一起工作得很好。所以你可以这样做:

Public Class Customer

  ' only usable by EF
  Private Sub New()
  End Sub

  ' what you or your class consumers will use
  Public Sub New(firstName As String, lastName As String)
      Me.New()
      ' assign properties, etc.
  End Sub

  ' etc.

End Class

In the current version of EF as of this writing, while you do need to have a parameterless constructor, it appears to work fine with a private parameterless constructor. So you can do this:

Public Class Customer

  ' only usable by EF
  Private Sub New()
  End Sub

  ' what you or your class consumers will use
  Public Sub New(firstName As String, lastName As String)
      Me.New()
      ' assign properties, etc.
  End Sub

  ' etc.

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文