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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:41:56+00:00 2026-06-10T03:41:56+00:00

I have an Entity Framework method which simply adds a Product to the database.

  • 0

I have an Entity Framework method which simply adds a Product to the database. When I run my test method this all works as expected.

I then have an AddProductAsync method:

Task<Product> AddProductAsync(Product pToAdd)
{
    return Task<Product>.Factory.StartNew(() => AddProduct(pToAdd));
}

When I run the test using this method the test runs but nothing actually happens. The break point in my AddProduct method is never hit and nothing is added to the database. There is no error, no warning.

What am I doing wrong? Or shall I just wait for EF6?

Thanks in advance

Original Method:

public static Product AddProduct(Product pToAdd)
    {
        using (var viralDatabase = new ViralWearContext())
        {
            if (viralDatabase.Products.Any(p => p.Name == pToAdd.Name || p.Description == pToAdd.Description))
            {
                var result = viralDatabase.Products.First(p => p.Name == pToAdd.Name || p.Description == pToAdd.Description);
                //set 0 so client knows it already exists
                result.ProductID = 0;
                return result;
            }
            else
            {
                var result = viralDatabase.Products.Add(pToAdd);
                viralDatabase.SaveChanges();
                return result;
            }
        }
    }

Method called by Unit Test:

public async void AddProductsAsyncTest()
    {
        Stopwatch timer = new Stopwatch();

        Product testProduct = GenerateTestProduct();
        Product testProductTwo = GenerateTestProduct();
        Product testProductThree = GenerateTestProduct();
        timer.Start();

        Product_Datalayer.AddProduct(testProduct);
        Product_Datalayer.AddProduct(testProductTwo);
        Product_Datalayer.AddProduct(testProductThree);
        var time1 = timer.ElapsedMilliseconds;
        Console.WriteLine(timer.ElapsedMilliseconds.ToString());

        timer.Reset();

        Product testProductAsync = GenerateTestProduct();
        Product testProductTwoAsync = GenerateTestProduct();
        Product testProductThreeAsync = GenerateTestProduct();
        timer.Start();

        var result1 = Product_Datalayer.AddProductAsync(testProductAsync);
        var result2 = Product_Datalayer.AddProductAsync(testProductTwoAsync);
        var result3 = Product_Datalayer.AddProductAsync(testProductThreeAsync);

        await Task.WhenAll(result1, result2, result3);
        var time2 = timer.ElapsedMilliseconds;
        Console.WriteLine(timer.ElapsedMilliseconds.ToString());
    }
  • 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-10T03:41:58+00:00Added an answer on June 10, 2026 at 3:41 am

    You need to make your unit tests async Task, not async void. Note that async unit tests are only supported by VS2012.

    If you’re on VS2010, you can use the Async Unit Tests project described here. I still recommend you use async Task, so they will translate easily to VS2012 (which is not supported by the Async Unit Tests project).

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

Sidebar

Related Questions

I have a WinForms application which is using Entity Framework to query a database.
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I have an Entity in Code First Entity framework that currently looks like this:
We have a WCF service that uses Entity Framework to query a SQL database.
I have been using Entity Framework 4.3 on an existing database and I have
I have this really strange problem where my entity framework query isn't enumerating correctly.
I have an ASP.NET MVC application which uses Entity Framework to get data. I
I have a WCF Data Service that exposes an Entity Framework database context and
I have a entity framework driven solution, on top of which, I have a
I have several Entity Framework Code First DbContext objects that use a custom Initializer.

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.