0

I am trying to locate the data type for DbContextOptionsBuilder.

I have an Entity Framework DBContext file. I setup an InMemory database with NUnit. How can I set this up without var? I know its best practice to have a clean database for each test, but just trying to learn, and have a database for multiple tests.

Electronics DBContext File

public partial class ElectronicsContext : DbContext
{
    public ElectronicsContext()
    {
    }

    public ElectronicsContext(DbContextOptions<ElectronicsContext> options)
        : base(options)
    {
    }

public virtual DbSet<Product> Product { get; set; }
public virtual DbSet<ProductCategory> ProductCategory { get; set; }

NUnit Test

public class TestClass
{

[SetUp]
public void TestProducts()
{

   // This Works
   // var context = new DbContextOptionsBuilder<ElectronicsContext>()
   //     .UseInMemoryDatabase(databaseName: "ProductsTest")
   //     .Options;

   // This does not work
    DbContextOptionsBuilder<ElectronicsContext> context = new DbContextOptionsBuilder<ElectronicsContext>()
        .UseInMemoryDatabase(databaseName: "ProductsTest")
        .Options;

When Running this: I receive error below, however it works other way Moq and setting up DB Context

    [Test]
    public void TestProducts()
    {
        DbContextOptionsBuilder<ElectronicsContext> context = new DbContextOptionsBuilder<ElectronicsContext>()

        context.Product.Add(new Product { ProductId = 1, ProductName = "TV", ProductDescription = "TV testing", ImageLocation = "test" });
        context.SaveChanges();

Error: 'DbContextOptionsBuilder<ElectronicsContext>' does not contain a definition for 'Product' and no accessible extension method 'Product' accepting a first argument of type 'DbContextOptionsBuilder<ElectronicsContext>' could be found (are you missing a using directive or an assembly reference?) 
  • `DbContextOptionsBuilder context` should be `DbContextOptions` as you assigning the `.Options` at the end, isn't it? Look at the second question you have asked https://stackoverflow.com/questions/52810039/moq-and-setting-up-db-context... – Johnny Oct 15 '18 at 05:58
  • ok, thanks it worked, do you know by any chance? started programming last month, I thought I was instantiating it as DbContextOptionsBuilder, –  Oct 15 '18 at 06:13
  • C# is static/strongly-typed however they have `var` as they call it syntactic sugar. This is just a shortcut provided by the language to reduce the amount of code in some common situations... – Johnny Oct 15 '18 at 06:23
  • yeah, I know that, just wondering why it worked as DbContextOptions, but not as DbContextOptionsBuilder, when we are instantiating it as a new DbContextOptionsBuilder.. –  Oct 15 '18 at 06:29
  • oh well, I'll figure it out, something new people are always trying to learn –  Oct 15 '18 at 06:30

1 Answers1

0

The problem is that you are trying to assign object of the type DbContextOptions to variable of the type DbContextOptionsBuilder<ElectronicsContext>. Look at the last line of the assignment, you have .Options;

// This does not work
DbContextOptionsBuilder<ElectronicsContext> context = new DbContextOptionsBuilder<ElectronicsContext>()
    .UseInMemoryDatabase(databaseName: "ProductsTest")
    .Options;

As C# is static/strongly-typed it is not allowed and the compiler will complain about it. However starting from c# 3.0 var is introduced. It is nothing else than syntactic sugar, the shortcut provided by the language to reduce amount of the code that have to be written in some common situations.

Your example is one of the examples where you are encouraged to use var, compare the lenght of the lines and readability...

Without var

DbContextOptionsBuilder<ElectronicsContext> context = new DbContextOptionsBuilder<ElectronicsContext>()

With var

var context = new DbContextOptionsBuilder<ElectronicsContext>()
Johnny
  • 8,939
  • 2
  • 28
  • 33
  • ok, I will have to read up what .options does to the data type, I am guessing it converts DbContextOptionsBuilder to DbContextOptions –  Oct 15 '18 at 06:40
  • 2 tips for you, builder pattern and https://learn.microsoft.com/en-us/ef/core/api/microsoft.entityframeworkcore.dbcontextoptionsbuilder ;) – Johnny Oct 15 '18 at 06:41
  • added comment above, just had time to retest, –  Oct 17 '18 at 03:48