实体框架参数化/重载构造函数?
我见过类似的问题,但它们并不完全是我所指的(或者也许它们是,但我不明白答案)
在我之前使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有生成任何默认构造函数,但这并不意味着它没有。但是,当您在没有显式设置无参数构造函数的类中定义带参数的构造函数时,它将隐藏默认构造函数。
因此,在您的分部类中,您只需要定义一个无参数构造函数。
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.
在撰写本文时的 EF 当前版本中,虽然您确实需要有一个无参数构造函数,但它似乎与 private 无参数构造函数一起工作得很好。所以你可以这样做:
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: