Category: .NET

  • OpenXml Validation Spreadsheet Value Between Two Numbers – C#

    Hi everyone, Just a quick post on how to validate a numeric cell to ensure that the value is between two numbers when using OpenXml: // Restrict min and max values var dataValidations = new DocumentFormat.OpenXml.Spreadsheet.DataValidations { Count = 0 }; DocumentFormat.OpenXml.Spreadsheet.DataValidation dataValidation; dataValidation = new DocumentFormat.OpenXml.Spreadsheet.DataValidation { Type = DataValidationValues.Decimal, AllowBlank = false, SequenceOfReferences…

  • View Raw SQL in EF Core – Simple Option

    Hi everyone, I’ve been looking for a simple way of viewing the raw output of sql in my local environment without having to make code changes and came across the following config setup: { “Logging”: { “LogLevel”: { “Default”: “Debug”, “System”: “Information”, “Microsoft”: “Information” } } This will show the sql statements in your output…

  • NetTopologySuite Circular Reference with .net core 2.2

    Hey everyone, Testing out a spatial project with .net core and I ran into the following error: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : The best overloaded method match for ‘Xunit.Assert.Equal(string, string)’ has some invalid arguments at CallSite.Target(Closure , CallSite , Type , Nullable`1 , Object ) at UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2) at DiscussionsControllerIntegrationTests.CreateTestDiscussion(HttpClient client, ApplicationDbContext…

  • .NET Core 2.1 is Returning JSON with Lowercase Properties

    Hi everyone, Just testing out .Net Core 2.1 and noticed that all of my JSON properties are being returned in lowercase despite being defined as uppercase. It turns out that this is configurable: // Change from this services.AddMvc(); // To this services .AddMvc() .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); Check out these links for more…

  • Dynamic Robots.txt with Web Api 2

    Hi everyone, For a project I’m currently working on I needed a dynamic robots.txt. Because our test environment is public facing we want to keep it from being indexed by Google etc. It took a bit of Googling to find a solution that worked, but in the end it was actually pretty simple. Here’s the…

  • DbSet does not contain a definition for ‘FromSQL’ and no extension method ‘FromSql’ accepting an argument of type ‘DbSet’ could be found.

    Hi everyone, I ran into the following error while attempting to use a custom query with EntityFramework: DbSet does not contain a definition for ‘FromSQL’ and no extension method ‘FromSql’ accepting an argument of type ‘DbSet’ could be found. This one’s pretty straight forward: // Install the following package via nuget Install-package Microsoft.EntityFrameworkCore.Relational //Add the…

  • Include UserId in Login Response (Token) – Web API 2

    Hi everyone, A quick post on how to include the user’s id in your login response when using Web API 2. The default response to the /Token request is as follows: { “access_token”: “xxxxxxxxxxxxx_xxxx”, “token_type”: “bearer”, “expires_in”: 1209599, “userName”: “test@test.com”, “.issued”: “Mon, 23 Apr 2018 06:08:03 GMT”, “.expires”: “Mon, 07 May 2018 06:08:03 GMT” }…

  • Cannot attach the file ‘C:…database.mdf’ as database x – Entity Framework

    Hi everyone, I ran into the following error when attempting to run ‘update-database’ on an initial migration: Cannot attach the file ‘C:Users…App_Dataaspnet-…115933.mdf’ as database ‘aspnet-…15933’ The solution to this one is pretty easy, remove the initial catalog property from your connection string. …33.mdf;Initial Catalog=aspnet… This is apparently caused by issues with EntityFramework and multiple projects…

  • Retrieving User Id in Web API 2 Controller – .NET

    Hi everyone, Just a quick post on how to retrieve the current user’s id in a Web API 2 controller: var userId = RequestContext.Principal.Identity.GetUserId(); Note that you’ll need the following using statements: using Microsoft.AspNet.Identity; using System.Web.Http; Thanks to the following stackoverflow post for the info: https://stackoverflow.com/a/21618056/522859

Create a website or blog at WordPress.com