In entity framework, how to make a string the main key!

entity-framework

Question

I have a legacy table containing employee name and employee id. Now the employee id is in char because it can hold values starting with an alphabet. Employee id is the only unique field available in the table.

Now I am using a view to capture the data from the table (It is in a separate database). But my entity framework is refusing to identify the view due to the lack of int primary key.

Any solution to this would be highly appreciated as I can not touch the legacy table.

1
6
1/22/2010 1:43:00 PM

Accepted Answer

But my entity framework is refusing to identify the view due to the lack of int primary key.

I doubt it. The EF supports strings as PKs.

But views, of course, can't have PKs at all. Which means you have to do some extra work when using them with the EF, especially if you make them updatable. Here's how.

6
1/22/2010 2:01:00 PM

Popular Answer

Create another column that is an int and use that as the primary key. The business users, don't need to know about that key.

My guess is the end application uses an Employee Id with charters. Like first and last initial plus hire date, something like DB012210.

How the table is done internally won't matter to the calling application so you can add another field as the real primary key which is an int. Then just put a unique constraint on the current EmployeeId, but not really use it as the primary key.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow