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 Basic
  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 Basic
csharpdatabaselinqsalesxml
5 Posts 2 Posters 0 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 Class

    L 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 Class

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Please don't post the same question in multiple forums. You already have some responses at http://www.codeproject.com/Messages/4544387/How-to-do-this-Relationship-in-Entity-Framework-us.aspx[^].

      Use the best guess

      A 1 Reply Last reply
      0
      • L Lost User

        Please don't post the same question in multiple forums. You already have some responses at http://www.codeproject.com/Messages/4544387/How-to-do-this-Relationship-in-Entity-Framework-us.aspx[^].

        Use the best guess

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

        True .. but (frazzle-me) suggested to submit it in the VB forum so I may get the answer .. so I thought it's ok to do that. Anyhow thanks for the info.

        L 1 Reply Last reply
        0
        • A ADSCNET

          True .. but (frazzle-me) suggested to submit it in the VB forum so I may get the answer .. so I thought it's ok to do that. Anyhow thanks for the info.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          A good reason to choose the correct forum in the first place.

          Use the best guess

          A 1 Reply Last reply
          0
          • L Lost User

            A good reason to choose the correct forum in the first place.

            Use the best guess

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

            Yes correct. But still I'm stuck & looking for the answer :)

            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