I ran into an issue recently where I needed to fix two fields in a SQL statement. Wasn’t a big deal except there were over 8,000 SQL statements requiring the change. Would have loved to do a simple Replace String in Notepad++ but each value was unique. The job that needed to be done was change a value from a string with single quotes around it to be a decimal number. Simple enough, a value like ‘12.34’ needs to be come 12.34 for the SQL statement to be valid.
Anytime I working with random text files I tend to work in Notepad++. I like the user experience it presents, it is light-weight, and fast. Within the application you can perform the standard search and replace or you can use regular expressions to search the file. I knew my search would have to involve regular expressions but what I wasn’t sure was whether I could replace the text with the cleaned version of itself. To my surprise, and due to regex ignorance, this was 100% possible. By taking advantage of a feature called Capture Groups a user can specify a search pattern as well as the specific part of the pattern you want to keep. Then, for the replace value in the search and replace window you set the replacement value as \1 which represents the first, and in my case only, capture group.
What this all came down to was using the regular expression ‘(d\d\.d\d\)’ created a search term that looked for a two digit number with a decimal and two more digits after the decimal surrounded by single quotes. The digits and decimal will be saved as part of capture group one. Now I know there were probably much better ways to create a regex search term but this worked so I went with it. Thank you to StackOverflow for getting me squared away with this problem.
EnableCors attribute on the API controller you need to make sure that the forward slash at the end of the
origins parameter is removed.
[EnableCors(origins: "http://localhost:49332", headers:"*", methods:"*")]
[EnableCors(origins: "http://localhost:49332/", headers:"*", methods:"*")]
That is it. That single forward slash tripped me up. The error written to the web DevTools console was
Access to XMLHttpRequest at 'http://localhost:49342/api/users/name/smith/' from origin 'http://localhost:49332' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Hope this helps you waste a little less time.
Not all projects, or development teams, use Code First with Entity Framework for building and maintaining their database. There are plenty of reasons for doing it either way but that discussion is outside of the scope of this post. What this post is going to focus on is generating your EF classes in a Database First environment.
Microsoft has plenty of great documentation for developers and one such post that I used recently was Getting Started with EF Core on ASP.NET Core with an Existing Database. My setup is Visual Studio 2017 Community Edition and a .NET Core 2.1 project structure. The database is running on a free, local instance of SQL Server 2017 and consists of a handful of tables. To generate the classes I utilized the Package Manager Console (PMC) in Visual Studio and the Scaffold-DbContext command.
Scaffold-DbContext "Data Source=localhost\SQLEXPRESS;Initial Catalog=MyDatabase;Integrated Security=True;" Microsoft.EntityFrameworkCore.SqlServer -outputDir Repository -Verbose
The connection string is pretty basic. I’m telling it to connect to a database on my machine called MyDatabase and use my Windows Credentials for authentication. The files generated should go into the directory called Repository in my project. And for help with debugging any problems I include the -Verbose parameter.
Upon completion all generated files were open in my VS instance for inspection and located in the folder I set as the output destination. If this didn’t happend and you have an error message make sure that your project builds on its own. If the project doesn’t build then the Scaffold-DbContext won’t be able to generate the files. One thing to check before you submit your code to any repository is that in your Context.cs class the connection string used for Scaffold-DbContext is hard-coded into the file. If the code is going to a public repository you’ll want to make sure this line is removed.