Something I Learned This Week — Entity Framework Is Picky About Primary Keys

When a simple task turns out to be harder than anticipated

Mike Wolfe
CodeX

--

Photo by Dmitry Ratushny on Unsplash

Earlier this week, I was assigned a task that instructed me to add a couple of extra fields to an API response. Figuring this would be pretty easy, I immediately dove in, but soon realized that the “couple of extra fields” were actually tied to tables in a database. Naturally, this complicates things considerably, because it is a much bigger addition than just adding a couple of properties to an object. Essentially, I am now required to create objects that correspond to the database table (easy), create configurations that map the objects to the tables (again easy), and finally use fluent API to define the relationships between the tables (can be tricky). Let’s take a look at what I had to do to accomplish this mission.

First Things First

Fortunately for me, defining the relationships is dependent on the objects being built and the object-to-table mapping. Therefore, I could knock out the easy stuff first.

Note: Example code will be used since I cannot show what was actually used.

Creating the new table objects looked something like this:

--

--

Mike Wolfe
CodeX
Writer for

Software Developer, Tech Enthusiast, Runner. Current project http://sqlcheater.com/ Connect with me on LinkedIn: www.linkedin.com/in/michael-wolfe-176212125