POCOs

I started working through the POCO Template for Entity Framework found here.  I got through the exercise and have a pretty good idea of the what (if not the why) of POCOs.  I ran into a couple of gotchas:

 

If you spell

string inputFile = @"..\POCOTTemplateWalkthrough\Blogging.edmx";

wrong

You get this error:

Error             1                     Running transformation: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.IO.FileNotFoundException: Unable to locate file

   at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path)

   at Microsoft.VisualStudio.TextTemplating.VSHost.TextTemplatingService.ResolvePath(String path)

 

Once I got to step #9, I ran it and it worked.  I then created a new Console app to be my User Tier, changed the project type of  POCOTemplateWalkthrough to a Class Library, and moved the Program.cs file to the UI.

To my surprise, it did not compile:

I then had to add a reference to Entities (I thought my UI could be Entity-unaware?) to get it compile and run.  It compiled but when I ran it, I got the following error:

{"Violation of PRIMARY KEY constraint ‘PK_People’. Cannot insert duplicate key in object ‘dbo.People’.\r\nThe statement has been terminated."}

I then deleted the 1st record in the data table table to get it to work

 

I ran it again and it worked:

Since I did not specify the ID, it is defaulting to 0 (the tutorial needs to be fixed some).  I then looked at the database table and confirmed that there was no identity key:

I changed it to identity and updated the Entity Framework and POW!

Of course, another way to do it is to add this line of code:

person.ID = 2;

 

But who wants to keep track of Primary Keys on the Data Layer? That is what identity keys are for.  I am a fan of surrogate keys, not a fan of composite keys. 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: