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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:11:10+00:00 2026-05-16T21:11:10+00:00

I’m thinking about starting a new project using EF 4 and going through some

  • 0

I’m thinking about starting a new project using EF 4 and going through some articles, I found an article about EF with repository pattern and unit of work (http://blogs.msdn.com/b/adonet/archive/2009/06/16/using-repository-and-unit-of-work-patterns-with-entity-framework-4-0.aspx)

Looking at that article, it uses the ObjectContext as the UnitOfWork and it passes it to the Repository.

My question is what if I have 2 ObjectContext which mean I will have 2 unit of work, but I actually wants all the operation perform on those 2 context to be one single unit of work, is this scenario possible? I don’t want to call save on each context, I’d like it to be transactional …. without using transactionscope …

For example, I have a context that manages Operation Log and another context that manages Orders. Lets say In my business layer, I have a method called AddOrder(). AddOrder() will use the order context to create a new order, but it will also use the operation log context to create a new operation log entry. Since those are 2 context, I’ll have to call save on both context to commit …. maybe the only option is to have only one single context ….

EDIT: I meant 2 context of different types for example: OperationalLogContext and OrderContext.

  • 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-16T21:11:11+00:00Added an answer on May 16, 2026 at 9:11 pm

    Yep – i believe it’s possible.

    The kicker is how handle your Repositories.

    For example, each Repository should take a Context .. so just create one context and pass it to each repository.

    (code please!) Glad u asked 🙂

    public interface IOrderRepository
    {
        IQueryable<Order> FindAll();
    }
    
    public interface IOperationLogRepository
    {
        IQueryable<OperationLog> FindAll();
    }
    
    public interface IUnitOfWork
    {
        void Commit();
    }
    

    .

    public class SqlServerContext : ObjectContext, IUnitOfWork
    {
        public void SqlServerContext(string connectionString) 
            : base(connectionString)
        {
        }
    
        public void Commit()
        {
            this.SaveChanges();
        }
    
        // Your other POCO's and stuff here ..etc..
    }
    

    .

    public class OrderRepostiory : IOrderRepository
    {
        private readonly SqlServerContext _sqlServerContext;
        public void OrderRepostiory(SqlServerContext sqlServerContext)
        {
            _sqlServerContext = sqlServerContext;
        }
    
        public IQueryable<Order> FindAll()
        {
            _sqlServerContext.Orders;
        }
    }
    

    .. and finally, instantiation. Cause your a good boy/girl/rainbow unicorn, you would be using Dependency Injection ..

    public class SqlServerRegistry : Registry
    {
        public SqlServerRegistry(string connectionString)
        {
            For<SqlServerContext>()
                .HybridHttpOrThreadLocalScoped()
                .Use<SqlServerContext>()
                .Ctor<string>("connectionString")
                    .Is(connectionString);
    
            For<IOrderRepository>().Use<OrderRepository>();
            For<IOperationLogRepository>().Use<OperationLogRepository>();
        }
    }
    

    and because the SqlServerContext is defined as HttpOrThreadLocal, it will be instantied ONCE and reused in multiple Repositories.

    Don’t know or understand DI/IoC ?

    then this would also work….

    private SqlServerContext _sqlServerContext;
    private IOrderRepository _orderRepository;
    private IOperationLogRepository _operationLogRepository;
    
    [TestInitialize]
    public void TestInitialise()
    {
        _sqlServerContext = new SqlServerContext(
                 ConfigurationManager.AppSettings["connectionString"]);
        _orderRepository = new OrderRepository(_sqlServerContext);
        _operationLogRepository= new OperationLogRepository(_sqlServerContext);
    }
    
    [TestMethod]
    public void SomeTest()
    {
        // Arrange.
        const int count = 10;
    
        // Act.
        var orders = _orderRepository.FindAll().Take(10).ToArray();
    
        // Assert.
        Assert.IsNotNull(orders);
        CollectionAssert.AllItemsAreNotNull(orders);
        Assert.AreEqual(count, orders.Length);
    }
    

    once more, that’s all untested code which i just typed up, as I was thinking about answering this question.

    HTH.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.