开发者

EF 4.1 Table-per-Hierarchy

开发者 https://www.devze.com 2023-04-13 02:04 出处:网络
I\'m trying to implement a simple TPH example from http://msdn.microsoft.com/en-us/library/dd793152.aspx. I have two tables:

I'm trying to implement a simple TPH example from http://msdn.microsoft.com/en-us/library/dd793152.aspx. I have two tables:

PERSON

[PersonID] [int] IDENTITY(1,1) NOT NULL,
[PersonTypeID] [int] NOT NULL,
[Name] [varchar](50) NOT NULL,
[HourlyRate] [int] NULL

PersonType

[PersonTypeID] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NOT NULL

In EF designer, I follow the tutorial and create a new Entity called Employee and specify Person as the base type. Then move the HourlyRate property to Employee. In the Mapping Details window, I map the entity to the Person table and it properly maps HourlyRate property to the correct DB field. Then I set Person to abstract.

If I build it now without specifying a condition and a discriminator in the Employee entity, it builds fine. If I follow the tutorial and specify HourlyRate as the condition and use "Is" "Not Null" as the discriminator, it builds fine.

But I want to use PersonTypeID as the discriminator and an Employee should hav开发者_运维技巧e a PersonTypeID of 1. So I select PersonTypeID as the the condition field, "=" as the operator, and 1 as the value. When I build, VS tells me the it's successful but also has something in the Error window.

Error 3032: Problem in mapping fragments starting at line 798:Condition member 'Person.PersonTypeID' with a condition other than 'IsNull=False' is mapped. Either remove the condition on Person.PersonTypeID or remove it from the mapping.

I read in another article that I need to delete the PersonType navigation property in the Person Entity. So I tried that but still got the same error.

I thought I was able to get this to work before on another project but I'm not sure what changed for this one. The only thing different that I can think of is that I recently updated to EF 4.1.

This is what I have set up in the designer

EF 4.1 Table-per-Hierarchy

Any suggestions are greatly appreciated!


I figured it out. Adding the PersonType entity to the designer threw everything off. The key was to delete the PersonType navigation property as well as the PersonTypeID property. But since I included the PersonType entity, deleting PersonTypeID broke the foreign key constraint.

By not including PersonType entity, I can delete PersonTypeID and everything compiles successfully.

0

精彩评论

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