小编Moh*_*our的帖子

'*.deps.json'文件是否应与程序集或内部nuget包一起分发?

'*.deps.json'文件应该与程序集一起分发还是在nuget包中分发?

nuget-package .net-core

10
推荐指数
1
解决办法
228
查看次数

NET 5 和 EF:如何在服务中使用 AddPooledDbContextFactory 代替 DbContext

AddPooledDbContextFactory我最近在 NET 5 自学文章中发现了这个概念,并热衷于正确实施它。但是,我不确定如何将它与我通常使用的泛型一起使用。

我当前的设置示例:

public void ConfigureServices(IServiceCollection services)
   {
        services.AddDbContext<TestDbContext>(
                (s, o) => o.UseNpgsql(Configuration.GetConnectionString("DatabaseConnection"))
                           .UseLoggerFactory(s.GetRequiredService<ILoggerFactory>()));

// other code //
    }
Run Code Online (Sandbox Code Playgroud)

我的存储库通用:

    public class Repository<T> : IRepository<T> where T
    {
        private readonly TestDbContext _dbContext;
        public Repository(TestDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        

        public async Task Create(T entity)
        {
           await _dbContext.Set<T>().AddAsync(entity);
           await _dbContext.SaveChangesAsync();
        }

        // other methods //
   }
Run Code Online (Sandbox Code Playgroud)

作为示例,这是通过以下方式调用的:

public class WeatherForecastController : ControllerBase
{
    
    private readonly IRepository<Test> testRepo;

    public WeatherForecastController(IRepository<Test> testRepo)
    {
        this.testRepo= testRepo;
    }

    [HttpGet]
    public async …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-core asp.net-core .net-5

10
推荐指数
1
解决办法
9861
查看次数

git reset和git revert有什么区别?

嗨,我是git的新手,我不明白git reset和之间的基本区别是什么git revert.是否git revert还原了合并?

git

8
推荐指数
1
解决办法
5483
查看次数

ASP.NET Core Web应用程序(.NET Framework)中app.config文件的用途是什么?

在ASP.NET Core Web应用程序(.NET Framework)中,有一个app.config文件包含:

<configuration>
   <runtime>
      <gcServer enabled="true"/>
   </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

但是此文件在ASP.NET Core Web应用程序(.NET Core)项目中不可用.ASP.NET Core Web应用程序(.NET Framework)需要GCserver模式运行吗?

garbage-collection asp.net-core asp.net-core-1.0

8
推荐指数
1
解决办法
1165
查看次数

与Moq模拟懒惰的界面

我想要模拟懒惰的界面,但我得到了object reference not set to an instance of an object例外.

这是被测试的课程:

public class ProductServiceService : IProductServiceService
{
    private readonly Lazy<IProductServiceRepository> _repository;
    private readonly Lazy<IProductPackageRepository> _productPackageRepository;

    public ProductServiceService(
        Lazy<IProductServiceRepository> repository,
        Lazy<IProductPackageRepository> productPackageRepository)
    {
        _repository = repository;
        _productPackageRepository = productPackageRepository;
    }

    public async Task<OperationResult> ValidateServiceAsync(ProductServiceEntity service)
    {
        var errors = new List<ValidationResult>();

        if (!await _productPackageRepository.Value.AnyAsync(p => p.Id == service.PackageId))
            errors.Add(new ValidationResult(string.Format(NameMessageResource.NotFoundError, NameMessageResource.ProductPackage)));

       .
       .
       .

        return errors.Any()
            ? OperationResult.Failed(errors.ToArray())
            : OperationResult.Success();
    }
}
Run Code Online (Sandbox Code Playgroud)

这是测试课

[Fact, Trait("Category", "Product")]
public async Task …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing moq mocking

6
推荐指数
1
解决办法
3681
查看次数

如何获得小时之间的间隔并从开始到结束放入列表?

例如,我会得到一个小时的时间,类型DateTime是这样的

对于初学者

  1. 我的开始时间是 00:00
  2. 结束时间为 02:00

并且每次 30 分钟我都喜欢将值输入到一个List<DateTime> so 中,我怎样才能将值放入一个看起来像这样的列表中?

  • 00:00
  • 00:30
  • 01:00
  • 01:30
  • 02:00

我的代码

        DateTime starTime = new DateTime();
        DateTime endTimes = new DateTime();
        DateTime interval = new DateTime();
        List<DateTime> intervals = new List<DateTime>();
        starTime = DateTime.ParseExact(fulldate + "00:00",
                                "yyyy/MM/dd HH:mm",
                                CultureInfo.InvariantCulture);
        endTimes = DateTime.ParseExact(fulldate + "02:00",
                                "yyyy/MM/dd HH:mm",
                                CultureInfo.InvariantCulture); ;
        interval = starTime;
        for (int i = 0; i < 24; i++)
        {
            interval.AddHours(0.5);
            intervals.Add(interval);
            if (interval.ToString("HH:mm") == endTimes.ToString("HH:mm"))
            {
                break;
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c#

5
推荐指数
1
解决办法
93
查看次数

如何防止EnumDropDownListFor将0设置为optionLabel值?

我有一个enum下拉列表:

@Html.EnumDropDownListFor(model => model.Type, "-- Choose --", new { @class = "postfix" })
Run Code Online (Sandbox Code Playgroud)

为枚举下拉列表生成的html代码是:

<select data-val="true" data-val-required="Select type of ..." id="Type" name="Type" class="valid">
    <option selected="selected" value="0">-- Choose --</option>
    <option value="1">?Hotel</option>
    <option value="2">Flight</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我不希望optionLabel的值为0,因为此值使下拉列表有效并且不会显示任何错误消息.我怎么能阻止这个?

asp.net-mvc enums asp.net-mvc-5

4
推荐指数
1
解决办法
3869
查看次数

模拟Linq"任何"与Moq的谓词

我正在尝试AnyAsync使用以下代码在我的存储库中模拟方法,但存储库总是返回false.

签名AnyAsync是:

Task<bool> AnyAsync<TEntity>(Expression<Func<TEntiry, bool>> predicate)
Run Code Online (Sandbox Code Playgroud)

我尝试了以下设置:

1:

mockCustomerRepository.Setup(r => r.AnyAsync(c => c.Email == "some@one.com"))
   .ReturnsAsync(true);
Run Code Online (Sandbox Code Playgroud)

2:

Expression<Func<CustomerEntity, bool>> predicate = expr => 
    expr.CustomerPerson.Email == "some@one.com";

mockCustomerRepository.Setup(r => r.AnyAsync(It.Is<Expression<Func<CustomerEntity, bool>>>
   (criteria => criteria == predicate))).ReturnsAsync(true);
Run Code Online (Sandbox Code Playgroud)

3:

mockCustomerRepository.Setup(r => r.AnyAsync(It.IsAny<Expression<Func<CustomerEntity, bool>>>()))
    .ReturnsAsync(true);
Run Code Online (Sandbox Code Playgroud)

我的测试:

public class Test 
{
    Mock<ICustomerRepository> mockCustomerRepository;

    public Test()
    {
        mockCustomerRepository = new Mock<ICustomerRepository>();
    }

    [Fact]
    public async Task CustomerTest()
    {   
        var customer = ObjectFactory.CreateCustomer(email: "some@one.com");
        var sut = new CustomerService(mockCustomerRepository.Object);

        var result …
Run Code Online (Sandbox Code Playgroud)

c# linq unit-testing moq mocking

3
推荐指数
1
解决办法
1091
查看次数

无法获取UserManager类

我想做的是添加一个新的管理员用户,并为其分配管理员角色。所以..我去了方法Startup.cs类,Configure并编写了以下代码:

var context = app.ApplicationServices.GetService<ApplicationDbContext>();

// Getting required parameters in order to get the user manager
var userStore = new UserStore<ApplicationUser>(context);

// Finally! get the user manager!
var userManager = new UserManager<ApplicationUser>(userStore);
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误消息:

严重性代码说明项目文件行抑制状态错误CS7036没有给出与UserManager.UserManager(IUserStore,IOptions,IPasswordHasher,IEnumerable>,IEnumerable>,ILookupNormalizer,IdentityErrorDescriber,IServiceProvider,ILogger>的必需形式参数'optionsAccessor'相对应的参数)'FinalProject..NETCoreApp,Version = v1.0 C:\ Users \ Or Natan \ Documents \ Visual Studio 2015 \ Projects \ FinalProject \ src \ FinalProject \ Startup.cs 101有效

这个错误在这里使我丧命。.显然,我需要userManager来创建新用户,但是我无法初始化该东西。

c# user-roles asp.net-core-mvc asp.net-core

3
推荐指数
1
解决办法
1662
查看次数

通过Visual Studio 2017中的postbuild活动构建.net核心项目

我有项目A,其对项目的依赖B,但也从没有提及BA.我想建立和复制组件bin folder项目B,以bin folder项目A.我怎么能用post build事件做到这一点dotnet msbuild.

我找到了这个链接,但它适用于VS 2015及以下和MS-Build:

通过prebuild事件构建另一个项目而不添加引用

msbuild post-build-event visual-studio .net-core visual-studio-2017

0
推荐指数
1
解决办法
4384
查看次数