locked
Using the Identity Specification in adding item to a table RRS feed

  • Question

  • Hi all,

    I'm a beginner in ADO.NET and I'm trying to learn how to manage entities. 

    I've created two table in a Db (named Users and Teams) and linked in the edmx by an 1...1 association with some fields. These tables have both an ID column (Users has the UserID column and Teams has the TeamID column) that have been created in the DB with the property:

    • Identity Specification: Yes
    • Identity Increment: 1
    • Identity Seed: 1

    The tables are characterised by the following fields:

    Teams:

    • Name (name of the team)
    • TeamID (integer primary key)

    Users: 

    • Name (name of the user)
    • UserID (integer primary key)
    • TeamID (Team managed by the user 1...1 associated)
    • Email (string with email address)

    To create a new team and a new user to be added to these tables, I've written the following code:

    using (Test_DB_Entity context = new Test_DB_Entity())
    {
    Team team = context.Team.CreateObject();
    team.Name = teamName;
    context.Team.AddObject(team);
    
    Users user = context.CreateObject<Users>();
    user.Name = name;
    user.Email = email;
    user.TeamId = team.TeamId;
    
    context.User.AddObject(user);
    
    context.SaveChanges();
    }
    

    My expectation is that the context creates automatically the identifier for both the team and the user, but when the debugger executes the SaveChanges, the following exception is raised (this is my translation of the message, that was instead in my language):

    The entity in 'Test_DB_Entity.Team' are part of the relationship 'TeamUsers'. Found 0 'Users' correlated. 1 'Users' foreseen.

    What's wrong?

    Thank you for any reply.

     

     

    Sunday, June 26, 2011 10:44 PM

Answers

  • On 6/26/2011 6:44 PM, Leo Daga wrote:
    > Hi all,
    >
    > I'm a beginner in ADO.NET and I'm trying to learn how to manage entities.
    >
    > I've created two table in a Db (named Users and Teams) and linked in the
    > edmx by an 1...1 association with some fields. These tables have both an
    > ID column (Users has the UserID column and Teams has the TeamID column)
    > that have been created in the DB with the property:
    >
    >     * Identity Specification: Yes
    >     * Identity Increment: 1
    >     * Identity Seed: 1
    >
    > The tables are characterised by the following fields:
    >
    > Teams:
    >
    >     * Name (name of the team)
    >     * TeamID (integer primary key)
    >
    > Users:
    >
    >     * Name (name of the user)
    >     * UserID (integer primary key)
    >     * TeamID (Team managed by the user 1...1 associated)
    >     * Email (string with email address)
    >
    > To create a new team and a new user to be added to these tables, I've
    > written the following code:
    >
    > using  (Test_DB_Entity context =new  Test_DB_Entity())
    > {
    > Team team = context.Team.CreateObject();
    > team.Name = teamName;
    > context.Team.AddObject(team);
    >
    > Users user = context.CreateObject<Users>();
    > user.Name = name;
    > user.Email = email;
    > user.TeamId = team.TeamId;
    >
    > context.User.AddObject(user);
    >
    > context.SaveChanges();
    > }
    >
    > My expectation is that the context creates automatically the identifier
    > for both the team and the user, but when the debugger executes the
    > SaveChanges, the following exception is raised (this is my translation
    > of the message, that was instead in my language):
    >
    > The entity in 'Test_DB_Entity.Team' are part of the relationship
    > 'TeamUsers'. Found 0 'Users' correlated. 1 'Users' foreseen.
    >
    > What's wrong?
    >
    > Thank you for any reply.
    >
     
    It should something along the lines of this.
     
    var team = new Team();
     
    set team properties
     
    var user = new Users();
     
    set user properties
     
    team.users.Add(user);
     
    context.AddObject(team);
     
    context.SaveChanges();
     
    • Marked as answer by Leo Daga Monday, June 27, 2011 8:32 AM
    Monday, June 27, 2011 1:50 AM
  • On 6/26/2011 6:44 PM, Leo Daga wrote:
     
    <snipped>
     
    I may have had it backwards for you.
     
    But one on the entites is a parent to its child.
     
    Parent.Child.Add(child)
     
    EF will add the objects and tie them together. You don't mess with the
    primary or foreign key properties EF will do it.
     
    • Marked as answer by Leo Daga Monday, June 27, 2011 8:32 AM
    Monday, June 27, 2011 1:58 AM

All replies

  • On 6/26/2011 6:44 PM, Leo Daga wrote:
    > Hi all,
    >
    > I'm a beginner in ADO.NET and I'm trying to learn how to manage entities.
    >
    > I've created two table in a Db (named Users and Teams) and linked in the
    > edmx by an 1...1 association with some fields. These tables have both an
    > ID column (Users has the UserID column and Teams has the TeamID column)
    > that have been created in the DB with the property:
    >
    >     * Identity Specification: Yes
    >     * Identity Increment: 1
    >     * Identity Seed: 1
    >
    > The tables are characterised by the following fields:
    >
    > Teams:
    >
    >     * Name (name of the team)
    >     * TeamID (integer primary key)
    >
    > Users:
    >
    >     * Name (name of the user)
    >     * UserID (integer primary key)
    >     * TeamID (Team managed by the user 1...1 associated)
    >     * Email (string with email address)
    >
    > To create a new team and a new user to be added to these tables, I've
    > written the following code:
    >
    > using  (Test_DB_Entity context =new  Test_DB_Entity())
    > {
    > Team team = context.Team.CreateObject();
    > team.Name = teamName;
    > context.Team.AddObject(team);
    >
    > Users user = context.CreateObject<Users>();
    > user.Name = name;
    > user.Email = email;
    > user.TeamId = team.TeamId;
    >
    > context.User.AddObject(user);
    >
    > context.SaveChanges();
    > }
    >
    > My expectation is that the context creates automatically the identifier
    > for both the team and the user, but when the debugger executes the
    > SaveChanges, the following exception is raised (this is my translation
    > of the message, that was instead in my language):
    >
    > The entity in 'Test_DB_Entity.Team' are part of the relationship
    > 'TeamUsers'. Found 0 'Users' correlated. 1 'Users' foreseen.
    >
    > What's wrong?
    >
    > Thank you for any reply.
    >
     
    It should something along the lines of this.
     
    var team = new Team();
     
    set team properties
     
    var user = new Users();
     
    set user properties
     
    team.users.Add(user);
     
    context.AddObject(team);
     
    context.SaveChanges();
     
    • Marked as answer by Leo Daga Monday, June 27, 2011 8:32 AM
    Monday, June 27, 2011 1:50 AM
  • On 6/26/2011 6:44 PM, Leo Daga wrote:
     
    <snipped>
     
    I may have had it backwards for you.
     
    But one on the entites is a parent to its child.
     
    Parent.Child.Add(child)
     
    EF will add the objects and tie them together. You don't mess with the
    primary or foreign key properties EF will do it.
     
    • Marked as answer by Leo Daga Monday, June 27, 2011 8:32 AM
    Monday, June 27, 2011 1:58 AM
  • Thank you for the answer. 

    The code now runs properly, even if I've relaxed the referential constraint between the Parent and the Child (from 1...1 to 1...*) because I wasn't able to let the compiler accept the 1..1 relation between the ParentID property of the child and the Entity key of the parent. 

    Monday, June 27, 2011 8:39 AM