5

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?

0

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

5 Comments

+1 Indeed, you have to do this or EF won't be able to instantiate your class when materializing your queries!
Did you look at the Designer file(s) to see what the constructor is? How do you new up an instance of Accomplishment if you don't have the default constructor? So there must be a default constructor.
@Thomas: There isn't a default constructor created explicitly. As @Frant says, you'll have to define a simple empty parameterless constructor that calls the base constructor in order to do this.
What would that look like? Because the EF generated objects have lots of stuff that I am not aware of it that it populates by default that I might not know to include in the parameterless one.
@sah302: you don't need to include anything. Just create it with empty body. EF only needs this constructor to materialize the entity dynamically, as Adam said.
2

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

2 Comments

You will encounter problems with lazy-loading, if you make the parameterless constructor private. See the [requirents for POCO proxies][1] in the MSDN documentation: > A custom data class must have a public or protected constructor that does not have parameters. [1]: msdn.microsoft.com/en-us/library/dd468057.aspx
Oh, the usual formatting directives won't work in commets. Good to know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.