bubibubi / JetEntityFrameworkProvider

Microsoft Access (Jet) Entity Framework provider
89 stars 26 forks source link

How do I test JetEntityFrameworkProvider? #54

Closed it19862 closed 4 years ago

it19862 commented 4 years ago

Question. How do I test JetEntityFrameworkProvider?

Used.

Description The project is located in the folder - C: \ test . The database is located in the folder - C: \ test .

I'm trying to test the project. In line

Operations.cs contextDB.SaveChanges (); I get an error. Error. **"Inner Exception 1: SqlException: A network or instance-specific error occurred while establishing a connection to SQL Server. Server not found or unavailable. Make sure the instance name is correct and that remote connections are allowed on the SQL Server. (provider: TCP Provider, error: 0 - The remote computer refused this network connection.)

Inner Exception 2: Win32Exception: The remote computer refused this network connection "**

Operations.cs

using ConsoleApp.Model;

namespace ConsoleApp.Core
{
    public class OperationsCRUDAccessDB
    {

        public void InsertEntity()
        {
            using (ContextDB contextDB = new ContextDB())
            {

                Planet planet = new Planet()
                {
                    ID = 11,
                    PlanetName = "WithPK_PlanetNameTest",
                    DistanceFromSun = 654,
                    Inhabitance = "Inhabitance_1",
                };

                contextDB.Planets.Add(planet);
                contextDB.SaveChanges();
            }
        }

    }
}

ContextDB.cs

using System.Data.Entity;

namespace ConsoleApp.Model
{
    public class ContextDB : DbContext
    {
        public ContextDB() : base("DefaultConnection")
        {
           Database.SetInitializer<ContextDB>(null);
        }  
        public DbSet<Planet> Planets { get; set; }        
    }
}

Planet.cs

namespace ConsoleApp.Model
{
    [Table("Planet")]
    public class Planet
    { 
        public int ID { get; set; }
        public string PlanetName { get; set; }
        public int DistanceFromSun { get; set; }
        public string Inhabitance { get; set; }        
    }
}

Program.cs

using ConsoleApp.Core;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            OperationsCRUDAccessDB operations = new OperationsCRUDAccessDB();

            operations.InsertEntity();
        }
    }
}

UnitTest1.cs

using Microsoft.VisualStudio.TestTools.UnitTesting;

using ConsoleApp.Core;
using ConsoleApp.Model;
using System.Linq;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {   
        [TestMethod]
        public void InsertEntity_Test()
        {
            Planet planetOrig = new Planet()
            {
                ID = 11,
                PlanetName = "WithPK_PlanetNameTest",
                DistanceFromSun = 654,
                Inhabitance = "Inhabitance_1",
            };
            Planet planetResult;

            OperationsCRUDAccessDB operationsCRUDAccessDB = new OperationsCRUDAccessDB();
            operationsCRUDAccessDB.InsertEntity();

            using (ContextDB contextDB = new ContextDB())
            {
                planetResult = contextDB.Planets
                            .Where(c => c.PlanetName == "WithPK_PlanetNameTest")
                            .FirstOrDefault();
            }

            // Сравнение
            Assert.AreEqual(planetOrig, planetResult);
        }

    }
}

2020-07-26_15-21-34

bubibubi commented 4 years ago

It's a connection string issue. EF is trying to connect to SQL Server. Check the connection string "DefaultConnection" in App.config