Just a quick post on fixing up a migration error after I added new config to startup.cs via DI. The error after running add-migration was pretty unintuitive:
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
To find out a bit more I ran the following:
This revealed the fact that there was an issue with adding my config line. The error stated that I had to have an empty constructor which led me on a bit of a wild goose chase.
The solution was simply a missed entry in application.json. In my case it was an omitted “assembly” section. Having said that, I did come across a few instances where people had the same problem when omitting commas etc.
Hopefully that’s able to get you on the right track!
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 action in one of the API Controllers:
public class UtilitiesController : CustomBaseApiController
public HttpResponseMessage GetRobotsFile()
var resp = new HttpResponseMessage(HttpStatusCode.OK);
var stringBuilder = new StringBuilder();
// Allow bots in production
// Don't allow bots in non-production environments
resp.Content = new StringContent(stringBuilder.ToString());
Also need to add the following to your web.config so that the robots.txt file can processed by the routing handler. Without this IIS will attempt to serve it as a static file and will return a 404 when it’s not found:
I ran into the following error after adding model validation attributes to a database with existing values:
String or binary data would be truncated.
The statement has been terminated.
The solution is pretty straightforward if you’re happy to truncate the values. Simply run the query below, swapping Title for your column name, Products for your table name and 25 for your new column length:
SET Title = LEFT(Title, 25)
WHERE LEN(Title) > 25
You should then be able to run update-database without any issues:
Specify the ‘-Verbose’ flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201806240741543_product_validation].
Applying explicit migration: 201806240741543_product_validation.
Running Seed method.