C# 为什么方法中的代码要执行两次?

C# 为什么方法中的代码要执行两次?,c#,.net-core,entity-framework-core,C#,.net Core,Entity Framework Core,CustomerRepository.GetCustomers()方法中的代码执行两次。 为什么会发生这种情况? 在图中,我展示了在方法中执行代码的步骤。 说明。 代码出现在步骤“5”中。 在步骤“5”之后,代码转到步骤“6”。 在步骤“11”之后,调试器继续根据代码工作 图片-1 Picture-2 Picture-3 Program.cs using System.Threading.Tasks; namespace ConsoleAppCore { class Pr

CustomerRepository.GetCustomers()方法中的代码执行两次。
为什么会发生这种情况?

在图中,我展示了在方法中执行代码的步骤。

说明。
代码出现在步骤“5”中。
在步骤“5”之后,代码转到步骤“6”。
在步骤“11”之后,调试器继续根据代码工作


图片-1

Picture-2

Picture-3


Program.cs

using System.Threading.Tasks;

namespace ConsoleAppCore
{
    class Program
    {   
        static async Task Main(string[] args) // ++ 
        {
            // await TestMain();

            Test1 test1 = new Test1();
            await test1.TestMain();
        }       
    }
}
using System.Threading.Tasks;

// 
using NUnit.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

//
using ConsoleApp;
using ConsoleApp.Model;
using DBRepository.Interfaces;
using DBRepository.Repositories;
using DBRepository.Factories;

namespace ConsoleAppCore
{
    // NUnit тестирование 
    // пишем классы с атрибутом [TestFixture] 
    // пишем методы с атрибутом [Test] 
    [TestFixture]
    public class Test1
    {
        public Test1()
        {
            TestMain();
        }

        public Task TestMain()
        {            
            return GetCustomersTest_1();
        }       

        /// <summary>
        /// ____
        /// </summary>
        [TestMethod]
        public async Task GetCustomersTest_1()
        {
            SettingsService settingsService = new SettingsService();
            Settings settings = new Settings();

            // Получить строку подключения
            settings = settingsService.ReadFilfeT();
            string connectionString = settings.ConnectionString;

            IRepositoryContextFactory _iRepositoryContextFactory = new RepositoryContextFactory();
            ICustomerRepository _iCustomerRepository = new CustomerRepository(connectionString, _iRepositoryContextFactory);

            var customerList = await _iCustomerRepository.GetCustomers();
            string strTest = "";
        }
    }
using DBRepository.Interfaces;
using Models;

// 
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;



namespace DBRepository.Repositories
{
    public class CustomerRepository : BaseRepository, ICustomerRepository
    {
        // Constructor
        public CustomerRepository(string connectionString, IRepositoryContextFactory contextFactory) : base(connectionString, contextFactory)
        {

        }

        public async Task<List<Customer>> GetCustomers()
        {
            using (var context = ContextFactory.CreateDbContext(ConnectionString))
            {
                var query = context.Customers.AsQueryable();
                    query = query.Where(p => p.ContactName.Contains("Maria"));
                return await query.ToListAsync();
            }
        }
    }
}
Test1.cs

using System.Threading.Tasks;

namespace ConsoleAppCore
{
    class Program
    {   
        static async Task Main(string[] args) // ++ 
        {
            // await TestMain();

            Test1 test1 = new Test1();
            await test1.TestMain();
        }       
    }
}
using System.Threading.Tasks;

// 
using NUnit.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

//
using ConsoleApp;
using ConsoleApp.Model;
using DBRepository.Interfaces;
using DBRepository.Repositories;
using DBRepository.Factories;

namespace ConsoleAppCore
{
    // NUnit тестирование 
    // пишем классы с атрибутом [TestFixture] 
    // пишем методы с атрибутом [Test] 
    [TestFixture]
    public class Test1
    {
        public Test1()
        {
            TestMain();
        }

        public Task TestMain()
        {            
            return GetCustomersTest_1();
        }       

        /// <summary>
        /// ____
        /// </summary>
        [TestMethod]
        public async Task GetCustomersTest_1()
        {
            SettingsService settingsService = new SettingsService();
            Settings settings = new Settings();

            // Получить строку подключения
            settings = settingsService.ReadFilfeT();
            string connectionString = settings.ConnectionString;

            IRepositoryContextFactory _iRepositoryContextFactory = new RepositoryContextFactory();
            ICustomerRepository _iCustomerRepository = new CustomerRepository(connectionString, _iRepositoryContextFactory);

            var customerList = await _iCustomerRepository.GetCustomers();
            string strTest = "";
        }
    }
using DBRepository.Interfaces;
using Models;

// 
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;



namespace DBRepository.Repositories
{
    public class CustomerRepository : BaseRepository, ICustomerRepository
    {
        // Constructor
        public CustomerRepository(string connectionString, IRepositoryContextFactory contextFactory) : base(connectionString, contextFactory)
        {

        }

        public async Task<List<Customer>> GetCustomers()
        {
            using (var context = ContextFactory.CreateDbContext(ConnectionString))
            {
                var query = context.Customers.AsQueryable();
                    query = query.Where(p => p.ContactName.Contains("Maria"));
                return await query.ToListAsync();
            }
        }
    }
}
使用System.Threading.Tasks;
// 
使用NUnit.Framework;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
//
使用ConsoleApp;
使用ConsoleApp.Model;
使用DBRepository.Interfaces;
使用DBRepository.Repositories;
使用DBRepository.Factories;
名称空间控制台AppCore
{
//努尼特
//试验夹具
//试验
[测试夹具]
公共类Test1
{
公共测试1()
{
TestMain();
}
公共任务TestMain()
{            
返回getCustomerTest_1();
}       
/// 
/// ____
/// 
[测试方法]
公共异步任务GetCustomerTest_1()
{
SettingsService SettingsService=新的SettingsService();
设置=新设置();
// Получить строку подключения
设置=设置服务.ReadFilfeT();
字符串connectionString=settings.connectionString;
IRepositoryContextFactory\u IRepositoryContextFactory=new RepositoryContextFactory();
ICCustomerRepository\u ICCustomerRepository=新的CustomerRepository(connectionString,\u iRepositoryContextFactory);
var customerList=await_iccustomerrepository.GetCustomers();
字符串strTest=“”;
}
}
CustomerRepository.cs

using System.Threading.Tasks;

namespace ConsoleAppCore
{
    class Program
    {   
        static async Task Main(string[] args) // ++ 
        {
            // await TestMain();

            Test1 test1 = new Test1();
            await test1.TestMain();
        }       
    }
}
using System.Threading.Tasks;

// 
using NUnit.Framework;
using Microsoft.VisualStudio.TestTools.UnitTesting;

//
using ConsoleApp;
using ConsoleApp.Model;
using DBRepository.Interfaces;
using DBRepository.Repositories;
using DBRepository.Factories;

namespace ConsoleAppCore
{
    // NUnit тестирование 
    // пишем классы с атрибутом [TestFixture] 
    // пишем методы с атрибутом [Test] 
    [TestFixture]
    public class Test1
    {
        public Test1()
        {
            TestMain();
        }

        public Task TestMain()
        {            
            return GetCustomersTest_1();
        }       

        /// <summary>
        /// ____
        /// </summary>
        [TestMethod]
        public async Task GetCustomersTest_1()
        {
            SettingsService settingsService = new SettingsService();
            Settings settings = new Settings();

            // Получить строку подключения
            settings = settingsService.ReadFilfeT();
            string connectionString = settings.ConnectionString;

            IRepositoryContextFactory _iRepositoryContextFactory = new RepositoryContextFactory();
            ICustomerRepository _iCustomerRepository = new CustomerRepository(connectionString, _iRepositoryContextFactory);

            var customerList = await _iCustomerRepository.GetCustomers();
            string strTest = "";
        }
    }
using DBRepository.Interfaces;
using Models;

// 
using System.Threading.Tasks;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;



namespace DBRepository.Repositories
{
    public class CustomerRepository : BaseRepository, ICustomerRepository
    {
        // Constructor
        public CustomerRepository(string connectionString, IRepositoryContextFactory contextFactory) : base(connectionString, contextFactory)
        {

        }

        public async Task<List<Customer>> GetCustomers()
        {
            using (var context = ContextFactory.CreateDbContext(ConnectionString))
            {
                var query = context.Customers.AsQueryable();
                    query = query.Where(p => p.ContactName.Contains("Maria"));
                return await query.ToListAsync();
            }
        }
    }
}
使用DBRepository.Interfaces;
使用模型;
// 
使用System.Threading.Tasks;
使用System.Linq;
使用Microsoft.EntityFrameworkCore;
使用System.Collections.Generic;
命名空间DBRepository.Repositories
{
公共类CustomerRepository:BaseRepository、ICCustomerRepository
{
//建造师
公共CustomerRepository(字符串connectionString,IRepositoryContextFactory contextFactory):基本(connectionString,contextFactory)
{
}
公共异步任务GetCustomers()
{
使用(var context=ContextFactory.CreateDbContext(ConnectionString))
{
var query=context.Customers.AsQueryable();
query=query.Where(p=>p.ContactName.Contains(“玛丽亚”);
return wait query.ToListAsync();
}
}
}
}

问题出在测试代码中。 您正在从
程序.cs
执行
TestMain
两次。 请注意,
Test1
构造函数已经在内部调用了
TestMain
方法。完成后,您可以再次手动调用它。 假设手动调用一次
TestMain
,则无需从
Test1
构造函数调用它:

[TestFixture]
公共类Test1
{
公共测试1()
{
//评论这一点。
//TestMain();
}
公共任务TestMain()
{            
返回getCustomerTest_1();
}    
//...
希望这有帮助