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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problems verifying if an object exist on entity framework

Problems verifying if an object exist on entity framework

Scheduled Pinned Locked Moved Visual Basic
databasecsharpsql-serversysadmin
7 Posts 4 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.
  • D Offline
    D Offline
    desanti
    wrote on last edited by
    #1

    Hello ! I'm using vb.net with EF 6 and sql server 2008R2. I have one table on database Myobject ( id , code , name) I want to add new records but verifying if an object with the same code exist. These records that have the same code are not added.

    Dim mynewobject as Myobject
    For I=1 to 100
    newcode= InputBox("Input code", "Value")
    newname= InputBox("Input Name", "Value")
    if IsNothing(context.Myobjects.Where(Function(t1) t1.code=newcode).Firstordefault) then
    mynewobject=New Myobject
    mynewobject.code=newcode
    mynewobject.name=newname
    context.Myobjects.Add(mynewobject)
    end if
    Next
    context.savechanges

    Now let's suppose that i want to add these records :

    Id Code Name
    1 cd1 Name1
    2 cd2 Name2
    3 cd1 Name3

    The third record should not be added because has the same code with the first record that has been added before. But when i execute my code , the third record is added too. What can i do ? Thank you !

    Richard DeemingR L 2 Replies Last reply
    0
    • D desanti

      Hello ! I'm using vb.net with EF 6 and sql server 2008R2. I have one table on database Myobject ( id , code , name) I want to add new records but verifying if an object with the same code exist. These records that have the same code are not added.

      Dim mynewobject as Myobject
      For I=1 to 100
      newcode= InputBox("Input code", "Value")
      newname= InputBox("Input Name", "Value")
      if IsNothing(context.Myobjects.Where(Function(t1) t1.code=newcode).Firstordefault) then
      mynewobject=New Myobject
      mynewobject.code=newcode
      mynewobject.name=newname
      context.Myobjects.Add(mynewobject)
      end if
      Next
      context.savechanges

      Now let's suppose that i want to add these records :

      Id Code Name
      1 cd1 Name1
      2 cd2 Name2
      3 cd1 Name3

      The third record should not be added because has the same code with the first record that has been added before. But when i execute my code , the third record is added too. What can i do ? Thank you !

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Try:

      If context.Myobjects.All(Function(t1) t1.code <> newcode) AndAlso context.Myobjects.Local.All(Function(t1) t.code <> newcode) Then


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • D desanti

        Hello ! I'm using vb.net with EF 6 and sql server 2008R2. I have one table on database Myobject ( id , code , name) I want to add new records but verifying if an object with the same code exist. These records that have the same code are not added.

        Dim mynewobject as Myobject
        For I=1 to 100
        newcode= InputBox("Input code", "Value")
        newname= InputBox("Input Name", "Value")
        if IsNothing(context.Myobjects.Where(Function(t1) t1.code=newcode).Firstordefault) then
        mynewobject=New Myobject
        mynewobject.code=newcode
        mynewobject.name=newname
        context.Myobjects.Add(mynewobject)
        end if
        Next
        context.savechanges

        Now let's suppose that i want to add these records :

        Id Code Name
        1 cd1 Name1
        2 cd2 Name2
        3 cd1 Name3

        The third record should not be added because has the same code with the first record that has been added before. But when i execute my code , the third record is added too. What can i do ? Thank you !

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

        desanti wrote:

        What can i do ?

        Add a constraint in SQL server to prevent it.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        M 1 Reply Last reply
        0
        • L Lost User

          desanti wrote:

          What can i do ?

          Add a constraint in SQL server to prevent it.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Programming by error AAaaahhhhhhhhh. I agree that a constraint is valid but the problem is known in the business layer and should be trapped there, the DB constraint should only be insurance.

          Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

          L 1 Reply Last reply
          0
          • M Mycroft Holmes

            Programming by error AAaaahhhhhhhhh. I agree that a constraint is valid but the problem is known in the business layer and should be trapped there, the DB constraint should only be insurance.

            Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

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

            Mycroft Holmes wrote:

            Programming by error AAaaahhhhhhhhh.

            Adding constraints is done to keep the data correct and consistent. It is not "programming by error", it is about preventing them.

            Mycroft Holmes wrote:

            I agree that a constraint is valid but the problem is known in the business layer and should be trapped there, the DB constraint should only be insurance.

            There "should be" a decent database-design to begin with.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

            M 1 Reply Last reply
            0
            • L Lost User

              Mycroft Holmes wrote:

              Programming by error AAaaahhhhhhhhh.

              Adding constraints is done to keep the data correct and consistent. It is not "programming by error", it is about preventing them.

              Mycroft Holmes wrote:

              I agree that a constraint is valid but the problem is known in the business layer and should be trapped there, the DB constraint should only be insurance.

              There "should be" a decent database-design to begin with.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Eddy Vluggen wrote:

              It is not "programming by error", it is about preventing them.

              Certainly this is true of a competent developer but I have seen may apps where they are trapping the error returned from the database and then dealing with the issue in the BL instead of dealing with the error before it hits the database.

              Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

              L 1 Reply Last reply
              0
              • M Mycroft Holmes

                Eddy Vluggen wrote:

                It is not "programming by error", it is about preventing them.

                Certainly this is true of a competent developer but I have seen may apps where they are trapping the error returned from the database and then dealing with the issue in the BL instead of dealing with the error before it hits the database.

                Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

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

                Mycroft Holmes wrote:

                Certainly this is true of a competent developer but I have seen may apps where they are trapping the error returned from the database and then dealing with the issue in the BL instead of dealing with the error before it hits the database.

                Aw, the horror. :rolleyes:

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                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