开发者

Error 6017: The NavigationProperty '(propertyname)' on the type '(typename)' is the source of

开发者 https://www.devze.com 2023-04-08 00:15 出处:网络
Error 6017: The NavigationProperty \'(propertyname)\' on the type \'(typename)\' is the source of a generated property \'(otherpropertyname)\' which conflicts with a member of the same name.

Error 6017: The NavigationProperty '(propertyname)' on the type '(typename)' is the source of a generated property '(otherpropertyname)' which conflicts with a member of the same name.

OK, I'm fairly certain I understand why I'm getting this error message, but it is not obvious to me how to work around开发者_如何学Go it.

I have a table salesreps which links to a table territories with a simple foreign key relationship. The territories table gets updated via an automated feed, whereas the salesreps table is manually maintained through a web interface I am designing.

I don't want the reps to be deleted if the territory goes away; I intend to highlight them for manual corrective action in the UI I am building as orphaned reps needing a territory assignment.

The sales reps are defined by an ID that is only unique with a given territory (nothing I can do about this, way outside my control), if a territory is removed, I made the foreign key ON DELETE behavior set the territoryID value to null, and made the column nullable. I then created a computed column called territoryReferenceID on the salesreps table set to isnull(territoryID, 0), made it persisted, and created the primary key based off the repID and the territoryReferenceID columns, since I cannot make a nullable PK column (which I still think is lame, even if I understand why it is). In the database, this works fine, and if somehow two territories are deleted with the same repID at the same time (highly unlikely) I'm ok with a primary key violation error that I can trap.

Mapping this to EF gives me the aforementioned error. I don't know why EF has a problem with this, and I don't know how to make the problem go away. I want to keep the behavior as designed in the database schema.

How can I correct this issue?


Here's what I ended up doing:

I removed the relationships in the entity model and the navigation properties, intending to just use the key ids directly. This worked, until the next time I updated the model from the database, where it reintroduced the relationship and threw the error.

So I made a view of the data with only one territoryID column (referencing the computed column) and created stored procedures for creating, modifying, and removing records from the view.

This works. It's ugly, but that's what I get for trying to "save time" with EF.

0

精彩评论

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

关注公众号