Wednesday, March 13, 2013

Bad Idea Challenge #2

Consider the following database table:

CREATE TABLE Code
(
Code char(4) PRIMARY KEY
Description varchar(50)
)

It’s a brand new table in a database nobody is using but you.  You add it to an Entity Framework model and add a value to it, as in this test:

using(Model1DataContext dc = new Model1DataContext())
{
// Verify the table is empty:
Assert.AreEqual(0, dc.Codes.Count());

// Add and save a new record:
Code rcd = new Code() { Code = "X", Description = "A description" };
dc.Codes.Add(rcd);
dc.SaveChanges();

// That was one record. How many records do you think get returned?
Assert.AreEqual(1, dc.Codes.Count());
}

Surprise:  you’ll get 2 records back. 

Do you see why?

Hint:  if you inspect the two records, they’ll look superficially the same.  But there’ll be one key (ahem) difference. 

ETA:  No, the semantic key is not the Bad Idea.  But you’re close. 

Labels:

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home