EF 4.1 Code First. Table-per-type inheritance with different primary key name from its base class' primary key name

后端 未结 2 636
被撕碎了的回忆
被撕碎了的回忆 2021-01-19 05:42

Given this:

create table Location(
LocationId int identity(1,1) not null primary key,
Address nvarchar(max) not null,
City nvarchar(max) null,
State nvarchar         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 06:32

    Here is a workaround for this issue:

    Create a view for the derived table and map your entity class that view. Rename the key column in your view so that it matches the key column in the base table.

    eg:

    base table User (UserID, FirstName, LastName)

    derived table Manager (ManagerID, DepartmentID)

    Entity Framework fails to update Manager as the key column is different!

    solution:

    create view UserManager
    as
    select
    ManagerID as UserID,
    DepartmentID
    from Manager
    

    Then map the Manager class to the UserManager view, instead of to the Manager table.

提交回复
热议问题