Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Studio 2015 & .NET 4.6
  4. How to do this Relationship in Entity Framework using DataAnnotation not Fluent API

How to do this Relationship in Entity Framework using DataAnnotation not Fluent API

Scheduled Pinned Locked Moved Visual Studio 2015 & .NET 4.6
csharpdatabaselinqsalesxml
8 Posts 2 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ADSCNET
    wrote on last edited by
    #1

    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.Web

    Imports System.ComponentModel.DataAnnotations
    Imports System.ComponentModel.DataAnnotations.Schema

    Public 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 Integer

    Public Property OccupationName() As String
    

    End Class

    Public Class InvoiceHdr
    <Key, ScaffoldColumn(False)>
    Public Property InvoiceID() As Integer

    Public Property InvoiceNo() As String
    Public Property Amount() As Integer
    
    Public Property CustomerID As Integer
    

    End Clas

    D 1 Reply Last reply
    0
    • A ADSCNET

      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.Web

      Imports System.ComponentModel.DataAnnotations
      Imports System.ComponentModel.DataAnnotations.Schema

      Public 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 Integer

      Public Property OccupationName() As String
      

      End Class

      Public Class InvoiceHdr
      <Key, ScaffoldColumn(False)>
      Public Property InvoiceID() As Integer

      Public Property InvoiceNo() As String
      Public Property Amount() As Integer
      
      Public Property CustomerID As Integer
      

      End Clas

      D Offline
      D Offline
      David C Hobbyist
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • D David C Hobbyist

        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

        A Offline
        A Offline
        ADSCNET
        wrote on last edited by
        #3

        Thanks for ur quick reply But do you mean to replace my code with your code or add your code after my code line?

        D 1 Reply Last reply
        0
        • A ADSCNET

          Thanks for ur quick reply But do you mean to replace my code with your code or add your code after my code line?

          D Offline
          D Offline
          David C Hobbyist
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • D David C Hobbyist

            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

            A Offline
            A Offline
            ADSCNET
            wrote on last edited by
            #5

            Unfortunately it didn't work.

            D 1 Reply Last reply
            0
            • A ADSCNET

              Unfortunately it didn't work.

              D Offline
              D Offline
              David C Hobbyist
              wrote on last edited by
              #6

              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

              A 1 Reply Last reply
              0
              • D David C Hobbyist

                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

                A Offline
                A Offline
                ADSCNET
                wrote on last edited by
                #7

                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.

                A 1 Reply Last reply
                0
                • A ADSCNET

                  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.

                  A Offline
                  A Offline
                  ADSCNET
                  wrote on last edited by
                  #8

                  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 OccupationClassTable

                  Then 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.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups