DEV Community

Cover image for Building a Service to Get a City Name from a ZIP Code in .NET Core

Building a Service to Get a City Name from a ZIP Code in .NET Core

Jeremy Morgan on September 04, 2019

I'm looking for honest feedback on my content. Please comment or reach out to me on Twitter! Have you ever seen one of those dropdowns that popula...
Collapse
 
dinsmoredesign profile image
Derek D • Edited

A few errors here:

  1. In your screenshot, you name the DB "zipcodes.db", then you have us run a migration, which creates a "cities.db", created from the Context.

  2. Startup.cs references "zipcodes.db", instead of the "cities.db" references in CityContext.

  3. If we change everything to "cities.db" (or "zipcodes.db") and then try to import the data from a CSV, you can't override the existing Cities table because DB Browser won't allow you to import the CSV into an existing table (the DB created from the migration) if it doesn't have the same number of matching columns. You need to add the "CityZip" column to the CSV file first.

  4. If we create a new DB from DB Browser and import the CSV into it, EF will connect, but throw an error because the "CityZip" column that EF adds doesn't exist.

  5. You name your table on import "zips", but your app is looking for the "Cities" table.

  6. Make sure to not have your DB open in DB Browser, or EF will report that the DB is Locked.

Once you change all occurrences of "zipcode.db" in your code to "cities.db", add the blank CityZip column to the CSV and import it into the DB created from the migration, all should run nicely ;) You don't need to manually create the DB, the migration will output the file for you.

Collapse
 
jeremycmorgan profile image
Jeremy Morgan

Thank you for the helpful feedback. I clearly missed a few things while putting it together. I have made corrections to the article and will have the project hosted up on GitHub soon so people can take a deeper look at it.

Thanks again!

Collapse
 
dinsmoredesign profile image
Derek D • Edited

No problem! I was looking for some resources to learn .NET Core (I'm a front end dev at a .NET shop) and I'd saved your tutorial a while back to try out. The errors actually forced me to learn the material better, since I had to do some troubleshooting to get everything running :P

Collapse
 
jeremycmorgan profile image
Jeremy Morgan

It's not really about the service, but learning how to build it that's important. There may also be cases where people want to build their own to match certain requirements and this is a good place to start.

Collapse
 
bojadev profile image
bojadev

Nice tutorial, what about the data quality of simple map? I know zipcodes for instance in the UK are quite complex, I never found a provider (even paid service) that had good quality data.

Collapse
 
jeremycmorgan profile image
Jeremy Morgan

I don't know much about it, my initial spot checks of ones in this area were pretty accurate. From what I understand the most accurate and updated version of it is the one you pay for.

I have not researched any UK databases, it sounds like a good business opportunity for someone who gets it right!