开发者

How to make string as primary key in entity framework!

开发者 https://www.devze.com 2022-12-17 15:05 出处:网络
I have a legacy 开发者_如何学Pythontable 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 f

I have a legacy 开发者_如何学Pythontable 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消