2 Replies - 129 Views - Last Post: 14 December 2018 - 10:25 PM Rate Topic: -----

#1 rostamiani   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-December 18

Entity Framework 'Add' function adds two objects instead of on

Posted 14 December 2018 - 10:06 PM

I'm using Entity Framework 6 with VB.NET.

The problem is that when I add a new 'user' object to the context, an additional user is added.

This is the code:

Dim Db As New DbModel

' Create a new user
Dim NewUser = New User With {
    .UserName = txtUserName.Text.Trim,
    .Password = txtPassword.Text.Trim,
    .Department = cmbDepartment.SelectedValue,
    .UserGroup = CmbUserGroup.SelectedValue,
    .Owner = CurrentUser,
    .Enabled = True
}

' Add new user to the model
Db.Users.Add(NewUser)


See the count of users before and after adding

Posted Image

The first user that is added is NewUser object and the second one is CurrentUser!!!

Is this a bug?

Is This A Good Question/Topic? 0
  • +

Replies To: Entity Framework 'Add' function adds two objects instead of on

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14693
  • View blog
  • Posts: 58,724
  • Joined: 12-June 08

Re: Entity Framework 'Add' function adds two objects instead of on

Posted 14 December 2018 - 10:10 PM

Have your printed out all the objects in the collection, or checked it out in the watch table? See what's being added?
Was This Post Helpful? 0
  • +
  • -

#3 rostamiani   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 14-December 18

Re: Entity Framework 'Add' function adds two objects instead of on

Posted 14 December 2018 - 10:25 PM

View Postmodi123_1, on 14 December 2018 - 10:10 PM, said:

Have your printed out all the objects in the collection, or checked it out in the watch table? See what's being added?


I checked 'Db.Users.Local.ToList'
The first user is the object that I added and the second extra one is 'CurrentUser' object!!!
As you can see 'CurrentUser' is one of properties of NewUser.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1