How to do this Relationship in Entity Framework using DataAnnotation not Fluent API
-
Dear All, I have 6 tables created from a class using EF Code First approach, and I'm wondering how to do the relationship between the tables using DataAnnotation, classes as follow:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.WebImports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.SchemaPublic Class Customer
<Key, ScaffoldColumn(False)>
Public Property CustomerID() As Integer<Display(Name:="File No"), StringLength(6)> Public Property CustomerFileNo() As String <Required(ErrorMessage:="First Name is required")> <Display(Name:="First Name"), StringLength(20)> Public Property FirstName() As String <Display(Name:="Last Name"), StringLength(20)> Public Property LastName() As String <Display(Name:="Street"), StringLength(40)> Public Property Addr\_StreetName() As String <DefaultSettingValue("0")> Public Property MaritalStatusID() As Integer <DefaultSettingValue("0")> Public Property Addr\_AreaID() As Integer <DefaultSettingValue("0")> Public Property Wrk\_OccupationID() As Integer
End Class
Public Class MaritalStatus
<Key, ScaffoldColumn(False)>
Public Property MaritalStatusID() As Integer<Required(ErrorMessage:="Name Required")> <Display(Name:="Marital Status"), StringLength(10)> Public Property MaritalName() As String <DefaultSettingValue("1")> Public Property RecStatus() As Boolean
End Class
Public Class Area
<Key, ScaffoldColumn(False)>
Public Property AreaID() As Integer<Required(ErrorMessage:="Name Required")> <Display(Name:="Area Name"), StringLength(30)> Public Property AreaName() As String <DefaultSettingValue("1")> Public Property RecStatus() As Boolean
End Class
Public Class Occupation
<Key, ScaffoldColumn(False)>
Public Property OccupationID() As IntegerPublic Property OccupationName() As String
End Class
Public Class InvoiceHdr
<Key, ScaffoldColumn(False)>
Public Property InvoiceID() As IntegerPublic Property InvoiceNo() As String Public Property Amount() As Integer Public Property CustomerID As Integer
End Clas
-
Dear All, I have 6 tables created from a class using EF Code First approach, and I'm wondering how to do the relationship between the tables using DataAnnotation, classes as follow:
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.WebImports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.SchemaPublic Class Customer
<Key, ScaffoldColumn(False)>
Public Property CustomerID() As Integer<Display(Name:="File No"), StringLength(6)> Public Property CustomerFileNo() As String <Required(ErrorMessage:="First Name is required")> <Display(Name:="First Name"), StringLength(20)> Public Property FirstName() As String <Display(Name:="Last Name"), StringLength(20)> Public Property LastName() As String <Display(Name:="Street"), StringLength(40)> Public Property Addr\_StreetName() As String <DefaultSettingValue("0")> Public Property MaritalStatusID() As Integer <DefaultSettingValue("0")> Public Property Addr\_AreaID() As Integer <DefaultSettingValue("0")> Public Property Wrk\_OccupationID() As Integer
End Class
Public Class MaritalStatus
<Key, ScaffoldColumn(False)>
Public Property MaritalStatusID() As Integer<Required(ErrorMessage:="Name Required")> <Display(Name:="Marital Status"), StringLength(10)> Public Property MaritalName() As String <DefaultSettingValue("1")> Public Property RecStatus() As Boolean
End Class
Public Class Area
<Key, ScaffoldColumn(False)>
Public Property AreaID() As Integer<Required(ErrorMessage:="Name Required")> <Display(Name:="Area Name"), StringLength(30)> Public Property AreaName() As String <DefaultSettingValue("1")> Public Property RecStatus() As Boolean
End Class
Public Class Occupation
<Key, ScaffoldColumn(False)>
Public Property OccupationID() As IntegerPublic Property OccupationName() As String
End Class
Public Class InvoiceHdr
<Key, ScaffoldColumn(False)>
Public Property InvoiceID() As IntegerPublic Property InvoiceNo() As String Public Property Amount() As Integer Public Property CustomerID As Integer
End Clas
ADSCNET wrote:
<DefaultSettingValue("0")> Public Property MaritalStatus() As Integer
Public Property MaritalStatus() As MartialStatus
And the same for the other navigation properties. Hope this helps.
Frazzle the name say's it all
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods
-
ADSCNET wrote:
<DefaultSettingValue("0")> Public Property MaritalStatus() As Integer
Public Property MaritalStatus() As MartialStatus
And the same for the other navigation properties. Hope this helps.
Frazzle the name say's it all
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods
-
Thanks for ur quick reply But do you mean to replace my code with your code or add your code after my code line?
http://visualstudiomagazine.com/articles/2012/03/07/~/media/ECG/visualstudiomagazine/Images/2012/03/wcovb\_EFCodeFirstNasr4.ashx I don't use vb but here is a link to help. Edit: I fixed the code in My first reply.
Frazzle the name say's it all
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods
-
http://visualstudiomagazine.com/articles/2012/03/07/~/media/ECG/visualstudiomagazine/Images/2012/03/wcovb\_EFCodeFirstNasr4.ashx I don't use vb but here is a link to help. Edit: I fixed the code in My first reply.
Frazzle the name say's it all
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods
-
Did You read the article from my earlier reply? Perhaps try the Visual Basic[^] forum.
Frazzle the name say's it all
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods
-
Did You read the article from my earlier reply? Perhaps try the Visual Basic[^] forum.
Frazzle the name say's it all
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
John F. Woods
Yes I did, what happened after I changed my module that it was expecting me to enter the MaritalStaus data from the Customer form it self & when I tried to use BindItem it gave me error like the value is not listed .... I'll try the VB forum. Thanks again.
-
Yes I did, what happened after I changed my module that it was expecting me to enter the MaritalStaus data from the Customer form it self & when I tried to use BindItem it gave me error like the value is not listed .... I'll try the VB forum. Thanks again.
I am submitting the answer I found so it could help whoever faced the same problem. in the Customer class need to add 2 lines for each property has a relation with this Customer class like Marital Status where we should get the list from the MaritalStatus table and save the selected value only in the Customer table:
<DefaultSettingValue("0")>
Public Property MaritalStatusID() As Integer?
Public Overridable Property MaritalStatus() As MaritalStatusClassTable ' This will generate a FK in the Customer table and populate the relationship.<DefaultSettingValue("0")>
Public Property Addr_AreaID() As Integer?
Public Overridable Property AreaList() As AreaListClassTable<DefaultSettingValue("0")>
Public Property Wrk_OccupationID() As Integer?
Public Overridable Property Occupation() As OccupationClassTableThen in the Customer FormView (if you are using FormView) you need only to define a SelectMethod function where it should return IEnumerable(Of xxxx) list of data from its master table. Note: I think the ID property which was created in the Customer class (Addr_AreaID(), MaritalStatusID(), Wrk_OccupationID()) should be the same ID of the PK in its master table. HTH.