我見過類似的問題,但他們不是我所指的(或者他們是,我不明白答案)實體框架參數化/重載的構造函數?
在我以前的應用程序使用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)
這似乎是在實體框架不再工作,因爲沒有默認的構造函數調用。
這不工作?如果是的話,執行這樣的事情有什麼好的選擇?
+1事實上,你*有*做到這一點,否則EF將無法實例化您的類時,實現您的查詢! – 2011-03-17 16:49:10
您是否看過Designer文件以查看構造函數是什麼?如果你沒有默認的構造函數,你如何創建一個'Accomplishment'的實例?所以必須有一個默認的構造函數。 – 2011-03-17 16:49:26
@Thomas:沒有明確創建的默認構造函數。正如@Frant所說的,你必須定義一個簡單的空無參數構造函數**來調用基礎構造函數**。 – 2011-03-17 16:50:22