0

I am putting together a new web application template for myself using .AspNetCore 2.0 and EFCore 2.0.

In my test project I am doing some testing with both creating the context and adding to DbSets.

Here is the context creation method.

public static ApplicationDbContext CreateDbContext(string[] args)
    {
        var basepath = GetBasePath("Application.UI");
        var configuration = new ConfigurationBuilder()
            .SetBasePath(basepath)
            .AddJsonFile("appsettings.json")
            .Build();
        //Set up configuration sources.

        var connectionString = configuration.GetConnectionString("DefaultConnection");
        var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
        builder.UseSqlServer(connectionString);

        return new ApplicationDbContext(builder.Options);
    }

Here is the test method where the error message is.

[TestMethod]
    public void AddContact_WithDates_ShouldGetAdded()
    {
        //Arrange
        var ct = new ContactUs
        {
            ContactWho = ContactWho.Support,
            Message = "Please call me about website problems",
            Email = "[email protected]",
            MessageSentTimestamp = DateTime.UtcNow,
            FirstName = "Jim",
            LastName = "Simons",
            ReportingError = false,
            MobilePhone = "5555556666"
        };
        var appArgs = new[] {"TestApp"};
        var context = ApplicationDbContextFactory.CreateDbContext(appArgs);
        //Act
        context.ContactUss.Add(ct);
        //Assert
        Assert.IsNotNull(context);  
    }

The context is created correctly. ContactUs (ContactUss is the DbSet of ContactUs) is just a standard domain class.

Context from immediate window

But VS2017 is giving me a red squiggly under ct in the Add(ct) call with the error message

Argument type ContactUs is not assignable to parameter of type TEntity

This has always been how I've worked with EF. Is there something new in Core that I'm missing?

3
  • there is nothing worng with that way, i'm using it too in my project and it works fine, I guess there is a problem somewhere else Commented Oct 26, 2017 at 12:57
  • First, this is an integration test, not a unit test. Second, DON'T TEST THE FRAMEWORK. You can safely assume that Add will in fact add the entity because EF Core is very well tested. It already has tests to ensure that functionality works correctly; creating your own is useless. Commented Oct 26, 2017 at 14:01
  • @Chris Pratt...The test was simply to verify that I could actually create the context and interact with it since I just started to use EF Core. I wasn't testing to see if the ADD method worked. Commented Oct 26, 2017 at 15:54

1 Answer 1

1

Well Visual Studio nor Resharper were alerting me to the solution for this error. But the error trace in the unit test UI showed System.Data.SqlClient file not found.

I added the Microsoft.EntityFrameworkCore.SqlServer package and this solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.