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 8031087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:58:37+00:00 2026-06-05T00:58:37+00:00

i am using Entity Framework and repository with unit of work design pattern. Sometimes

  • 0

i am using Entity Framework and repository with unit of work design pattern.

Sometimes with my unit of work, I am making other calls to a different service.

This service in turn is making its own unit of work instance, for example:

ISettingsService settingsService = UnityContainer.Resolve<ISettingService>();
using (IUnitOfWork unitOfWork = UnitOfWorkFactory.CreateUnitOfWork())
{
    List<Company> companies = unitOfWork.CompaniesRepository.GetAll();
    foreach(Company company in companies)
    {
        settingsService.SaveSettings(company, "value to set");
        company.Processed = DateTime.UtcNow();
    }
    unitOfWork.Save();
}

// ISettingsService.SaveSettings code in another module...
ISettingsService.SaveSettings(Company company, string value)
{
    using (IUnitOfWork unitOfWork = UnitOfWorkFactory.CreateUnitOfWork())
    {
        Setting setting = new Setting();
        setting.CompanyID = company.CompanyID;
        setting.SettingValue = value;
        unitOfWork.Insert(setting);
        unitOfWork.Save();
    }
}

The above code does work, but i have to reference ID’s explicitly rather than attaching the company object (it would throw an error as it is already being tracked by the other unit-of-work in the other service).

As far as I can tell I see 3 ways of this happening:

1) Use the code as-is with service layers creating their own unit-of-work instances, any other references to other entity’s are done on a primary key basis (procedures to set values should be passed by primary key object value, i.e. int customerID).

The bad side to this is potentially more hits to the database, and if the entity primary key type changed, i would need to change all service layer references for the ID field.
__

2) Have service layers accept entity objects as references. This would be nice as i could pass objects around.

The bad side to this is that entities must not be referenced from any other context and must be attached to context to where it will be used. In a loop, the entity will most probably already be attached.
__

3) Pass in unit-of-work instantiations to other service layers to they can use the existing unit-of-work instead of instantiating their own.

Not sure there is a downside to this other than having to pass around a unit-of-work reference, the upside would be the ability to reference objects without having to attach them or worry if they are already attached.
__

In summary, I need to standardize on one access design, and would appreciate recommendations on what one i should choose.

In addition is there anything with transactions that i need to be concerned about or is EntityFramework with the unit-of-work design pattern implementing a form of transactional processing implicitly as changed are only committed on context.Save(), or am i mistaken?

Thanks,

Chris

  • 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-05T00:58:40+00:00Added an answer on June 5, 2026 at 12:58 am

    You have a few options amongst them:

    1. Have different services pass out dettached entities (its entities), perhaps even DTOs that are not entities, then have each service translate them to its entities (or use primary keys).

      Each service can create its own content and use its own EDMX or a shared EMDX (or even several EDMXs).

    2. If the services are using the same EDMX, then inject the same context into them (you can wrap it behind and interface so that they don’t expose EF).

    3. If the services don’t use the same EDMX, either:

      3.1. Go with option 1, using different entities for different services (or DTOs).

      You can “share” tables between the EDMXs using different entities or use views to “tables from external domains” to reflect a read-only version of tables to other domains.

      3.2. Create a master EDMX by merging the EDMXs into one big one (see this question for details)

    Changes are applied only when calling context.AcceptChanges(), so you can save changes to multiple contexts, then accept changes in each after all the saves have completed.

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

Sidebar

Related Questions

I am using Entity Framework, POCO entities and repository pattern for data access. I
I'm getting into using the Repository Pattern for data access with the Entity Framework
I plan to write my first application using Entity Framework + Repository + Unit
I am using Entity Framework v4 and the repository pattern found in the answer
I am using Entity Framework 4 with the Service/Repository/EF4/POCO classes technique, and I have
i'm trying to implement a Repository and UnitOfWork patterns using Entity Framework. This is
I'm trying to implement the repository pattern using entity framework code first rc 1.
I am using Entity Framework 4.1 and the repository pattern . I am trying
I am using Entity Framework with the generic repository pattern. I have used the
I have a generic repository that is using Entity Framework 4 with the DbContext

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.