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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:34:06+00:00 2026-05-26T05:34:06+00:00

I have a scenario where the code under test inserts a record into a

  • 0

I have a scenario where the code under test inserts a record into a database, then attempts to retrieve it back from the database using its primary key.

This happens over a series of records in the same method.

I am mocking my ObjectContext and ObjectSets, so the current result is that each record “inserted” into my FakeObjectSet is given a primary key of 0.

In the case where I am only inserting a single record and asserting the record exists, this is fine…but when I am inserting multiple and my workflow requires retrieving specific records by primary key, my queries return multiple results since all inserts have a primary key of 0.

Any ideas?

  • 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-26T05:34:07+00:00Added an answer on May 26, 2026 at 5:34 am

    You can add an event to your FakeObjectSet<T> that will be raised when AddObject is called. Then in your test, add an event handler to set the Id of the object being added.

    For example, a simplified version of a fake object set:

    public class ObjectAddedEventArgs<T> : EventArgs
    {
        public T Item { get; set; }
    }
    
    public class FakeObjectSet<T>
    {
        private List<T> _list = new List<T>();
    
        public event EventHandler<ObjectAddedEventArgs<T>> ObjectAdded;
    
        public void AddObject(T item)
        {
            _list.Add(item);
            OnObjectAdded(item);
        }
    
        protected virtual void OnObjectAdded(T item)
        {
            EventHandler<ObjectAddedEventArgs<T>> h = AddingItem;
            if (h != null)
            {
                h(this, new ObjectAddedEventArgs<T> { Item = item });
            }
        }
    
        // other methods...
    }
    

    And in your test, when you want to set an incrementing Id value, add a handler to your FakeObjectSet<T> and update the object’s Id:

    int id = 0;
    
    FakeObjectSet<Foo> set = new FakeObjectSet<Foo>();
    set.ObjectAdded += (s, e) => { e.Item.Id = id++; };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: old legacy code in rpg have to consume data from a new web
I'd like to have all DB DDL code under CVS . We are using
We have a scenario where some .NET code is attempting to access the current
Okay so I have a scenario similar to the below code, I have a
I have the following scenario. The client code only has access to FooHandler, not
This is my current scenario: I have checked in my grails app code +
Say I have a simple client/server scenario with one method: // client code $client
Hey all. Here is the scenario. I have the below code, and I'm needing
I have a scenario, in which a .net application is hosted from SAP portal.
I have the following scenario where I’m planning in using windows authentication. 1.1) I

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.