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 following namespace to your file
using Microsoft.EntityFrameworkCore;

Hopefully that’ll solve it for you, but if not there’s a lot a more information in these posts:
https://stackoverflow.com/a/38919326/522859
https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/146

Module not found: Can’t resolve ‘babel-polyfill’ in ‘C:\Users\source\repos\frontend\src

Hi everyone,

I was looking into asynchronous requests with redux tonight and hit the following error:

Module not found: Can’t resolve ‘babel-polyfill’ in ‘C:\Users\source\repos\frontend\src

This is used to provide a promise polyfill for a fetch package mentioned in the tutorial. In order to fix it, just run the following:

npm install babel-polyfill

Thanks to the following post for the solution: https://stackoverflow.com/a/33568284/522859

Module not found: Can’t resolve ‘redux’ in ‘C:\Users\FrontEndSrc\frontend\node_modules\react-redux\es\connect’

Hi everyone,

Another quick one. I’d installed react-redux but was running into the following error:

Failed to compile.

./node_modules/react-redux/es/connect/mapDispatchToProps.js
Module not found: Can’t resolve ‘redux’ in ‘C:\Users\source\repos\FrontEndSrc\frontend\node_modules\react-redux\es\connect’

Fairly easy fix, you need react as well as react-redux:

npm install –save redux

Once that’s done, it should all be working!

Cheers,
Chris

index.js:2178 Warning: A component is changing an uncontrolled input of type checkbox to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components

Hi everyone,

Bit of a silly error I ran into today. This one took an embarrassingly long time to figure out unfortunately:

index.js:2178 Warning: A component is changing an uncontrolled input of type checkbox to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components

It turned out that the error was a result of not having added the state variable in the constructor. It was then being created onChange and converting the input element into a controlled input.

I was trying to use a checkbox in my form and had copied the input from another view:

  1. <label>Ticket</label>
    
  2. <input name="IsTicket" type="checkbox" checked={this.state.IsTicket} onChange={(event) => {this.handleChange(event)}} />

HandleChange is as follows:

/* Sets a state value based on the name of the input changed */
handleChange: function (caller, event) {

const target = event.target;
const value = target.type === 'checkbox' ? target.checked : target.value;
const name = target.name;

caller.setState({ [name]: value });
}

Bit of a silly one, but hopefully it will be able to help out anyone else who runs into it!

Cheers,
Chris

Warning: Can’t call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method. – ReactJs

Hi everyone,

This is just a quick post, mostly for my future reference but hopefully it will help someone else out as well. I ran into the following error when invoking a callback in a child component:

Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

This is generally a result of updating the state of a child component after the parent component has hidden it.

e.g. the child component calls a prop function which hides the child component. On the line after the prop function call the child component attempts to set a state variable.

Issue

In my case, it was because I was setting the loading state of a child component to false after performing a successful fetch. Normally this would be fine however I had hidden the child component via the parent component’s prop callback after the successful response was confirmed.

  • (Child Component): Set loading state to true.
  • (Child Component): Call API
  • (Child Component): After retrieving payload, invoke parent component’s prop function and pass it the payload
  • (Parent Component): Process payload. In render, the new payload hides the child component
  • (Child Component): Attempts to set loading state to false Error: can’t change state now that the component is no longer mounted

Solution

To get around it, I had to ensure that I didn’t invoke the parent’s callback (or unload the child) until after the child’s state had been set.

  • (Child Component): Set loading state to true.
  • (Child Component): Call API
  • (Child Component): Retrieve payload, set loading state to false place state change before parent callback is invoked
  • (Child Component): Invoke parent component’s prop function and pass it the payload
  • (Parent Component): Process payload. In render, the new payload hides the child component

Now that the child component state is modified before it is unmounted there isn’t an issue. The easiest way that I’ve found to debug errors like this is to set breakpoints (either hardcoded or in the console) to ensure that everything is being invoked in the expected order.

If you need any more info please let me know!

Thanks,
Chris

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”
}

Once the changes below have been made the response will include a userId field:

{
“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”,
“userId”: “xxxxxxx”
}

There are three very small changes required in order to add this functionality.

First, add an additional argument to CreateProperties in ApplicationOAuthProvider.cs

public static AuthenticationProperties CreateProperties(string userName, string userId)
{
IDictionary data = new Dictionary
{
{ "userName", userName },
// Add
{ "userId", userId }
};
return new AuthenticationProperties(data);
}

Pass userId to CreateProperties in ApplicationOauthProvider.

// ApplicationOAuthProvider.cs > GrantResourceOwnerCredentials
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);

// Add user id
AuthenticationProperties properties = CreateProperties(user.UserName, user.Id);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);

Pass userId to CreateProperties in AccountController.

// AccountController.cs > GetExternalLogin
if (hasRegistered)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
CookieAuthenticationDefaults.AuthenticationType);

// Add userid
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName, user.Id);
Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);

Module build failed: Error: ENOENT: no such file or directory, open – Windows

Hi everyone,

I’ve been mucking around with React over the last couple of days. I ran into the following error while trying to build:

Module build failed: Error: ENOENT: no such file or directory, open ‘c:\…Form.js’
at Error (native)

It took a while to sort this one out but I eventually came across a solution on github:

npm rebuild node-sass

Thanks to xzyfer for posting in the following github thread: https://github.com/sass/node-sass/issues/1579#issuecomment-227661284

There’s a whole heap of info in that thread so be sure to check it out.

Thanks,
Chris

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_Data\aspnet-…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 in the same database. See the following stackoverflow answer for more info: https://stackoverflow.com/a/20176660/522859

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

Git Ignore Template for .NET

Hi everyone,

A sample .gitignore file for if you’re working with .net:

*.cache
*.dll
*.exe
*.pdb
/build/
*.suo
*.user
_ReSharper.*/
*.sdf
*.opensdf
*.tlog
*.log
TestResult.xml
*.VisualState.xml
Version.cs
Version.h
Version.cpp

Also, if you happen to have added some files you didn’t mean to:

git rm –cached file_you_dont_want.pdb
git commit -m “Remove pdb file”

Thanks to these stackoverflow posts for the info:
https://stackoverflow.com/a/8675415/522859
https://stackoverflow.com/a/11629271/522859