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

  • Home
  • SEARCH
  • 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 6547343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:49:11+00:00 2026-05-25T11:49:11+00:00

I have two models in LINQ to SQL. First is Employee and the second

  • 0

I have two models in LINQ to SQL. First is Employee and the second is Position.
There is Position data that I already inserted in Position table. This data will be referenced by Employee model.

Position {
    int id,   //auto increament
    string name,
    string description, 
}

Employee {
    int id,
    string name,
    Position position,
}

The logic when I want to create employee is:

  1. I Crate EmployeCreate and Assign employee name, and position ID
  2. Find the position by Id using position repository.
  3. Create Employee model assign the founded position model from step two
  4. Insert the employe using employee repository and submit changes

I think my steps is correct, But The founded positions is insert in to database again, normally It is not inserted because the data already exists in the database.

My snippet service as follows:

EmployeService {
    //property and constructor

    AddEmployee(EmployeeCreate command) {
        var employee = EmployeeFactory.Create(command);
        var possition = _positionRepository.GetById(command.possitionId);

        using (var tx = new TransactionScope())
        {
            employee.Position = possition;
            _employeeRepository.InsertOnSubmit(employee);

            _employeeRepository.SubmitChanges();
            tx.Complete();
        }   
    }
}

EDIT
I create generic Linq repository. Employee and Position is extends the generic Linq repository.

public class LinqRepository<T> : IRepository<T>, IRepository where T : class
    {

        protected readonly DataContext _dataContext;

        public LinqRepository(IDataContextProvider dataContextProvider)
        {
            _dataContext = dataContextProvider.DataContext;
        }
}
  • 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-05-25T11:49:12+00:00Added an answer on May 25, 2026 at 11:49 am

    You shouldn’t need the TransactionScope here unless you are trying to submit changes across multiple contexts. SubmitChanges internally creates a transaction for the request to cover the multiple insert/update/delete operations requested as part of the submitchanges. The only time you need to explicitly manage the context is when you have multiple context/connections that are spanned by the call.

    The behavior you are seeing indicates that the Position may be fetched by a context other than the one that you are using when inserting the employee into the _employeeRepostiory.

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

Sidebar

Related Questions

I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that
I have two models: Person and Relation. The second one stores information about relations
I have two models, user and group. I also have a joining table groups_users.
I have two models, say Product and Bundle. products table is related to product_prices
I have an ASP.NET MVC 3 Web Application using Linq-to-SQL for my data access
If I have two tables... Category and Pet. Is there a way in LINQ
I have two models. We'll call them object A and object B. Their design
I have two models indexed for searching (User and Item). I'm trying to do
I have two models: Show and Venue . Show has one venue while each

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.