Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:33:22+00:00 2026-06-16T20:33:22+00:00

I need to add a record to a database using the Entity Framework. Since

  • 0

I need to add a record to a database using the Entity Framework. Since I’m brand new to using this syntax I am not sure how to properly write the code (Below is my best guess).

First, the agent must have their info inserted into the Agent table. This table produces a self-incrementing primary key known as a SymNumber. I then need to take that SymNumber and use it as a primary key for an insert into the AgentIdentification table.

I have run this code a couple of times, and I do not come up with an error, however since I am using a unit test to test the code I cannot tell for sure if the agent is being added properly. Secondly, I know for a fact that I am not correctly grabbing the SymNumber as generated by the Agent table after the first insert. The SymNumber is an int value in the Linq code set to 0, and this does not change during the AgentIdentification insert.

Any help would be greatly appreciated!

        AgentResourcesEntities _db = new AgentResourcesEntities();

        try
        {
            Agent agent = new Agent();
            agent.EntityType = "P";
            agent.FirstName = agentNewTraining.FirstName;
            agent.LastName = agentNewTraining.LastName;
            agent.LastChangeOperator = agentNewTraining.Requestor;
            agent.LastChangeDate = DateTime.Now;
            if (!String.IsNullOrEmpty(agentNewTraining.NameSuffix)) agent.NameSuffix = agentNewTraining.NameSuffix;

            _db.Agent.AddObject(agent);

            AgentIdentification agentIdentification = new AgentIdentification();
            agentIdentification.SymNumber = agent.SymNumber;
            agentIdentification.ReferenceType = "S";
            agentIdentification.DummyReference = 0;
            agentIdentification.LastChangeOperator = agentNewTraining.Requestor;
            agentIdentification.LastChangeDate = DateTime.Now;

            _db.AgentIdentification.AddObject(agentIdentification);

            return true;
        }
        catch (Exception)
        {
            return false;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:33:23+00:00Added an answer on June 16, 2026 at 8:33 pm

    First you need to call

    _db.SaveChanges();
    

    to get your changed persisted.

    But if you want also synchronize (get the new generated value) your agent.SymNumber you will need to call SaveChanges() right after adding it to context.

    So the code will be like:

    /// ...... ////
    _db.Agent.AddObject(agent);
    _db.SaveChanges();
    
    AgentIdentification agentIdentification = new AgentIdentification();
    agentIdentification.SymNumber = agent.SymNumber;  // sym number is now synchronized from DB
     ///...../////
    
    _db.AgentIdentification.AddObject(agentIdentification);
    _db.SaveChanges();
    

    But if SymNumber is foreign key so the AgentIdentification has could have reference to some Agent instance, you can just tie those instances with that reference and would not need to call that additional SaveChanges() in the middle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add record locking to a site. This isn't database locking where
Using ASP.NET MVC and Entity Framework, I encounter some attach/detach errors when I need
i'm trying to implement a Repository and UnitOfWork patterns using Entity Framework. This is
I need add a new user group for mediawiki. The new group has more
I can't access a element with its href. Like this example, i need add
Hї! I have wrote test for my application. I need add item to database
I am new to EF. I created entity models from database. I have tables
I recently started using Entity Framework, and it has been kind of a pain
I've been trying to get started using entity framework and have ran across a
I'm building my application with Entity Framework (model first principle). I'm also using MS

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.