错误2016年:条件不能为列成员指定能为、成员、错误、条件

2023-09-06 14:21:30 作者:——花落盡唯剩空城虛

我有在VS2010与实体框架的一些问题

I am having some issues with Entity Framework in VS2010

我收到描述得非常好这里的问题...

The problem I'm getting is described very well here...

http://social.msdn.microsoft.com/Forums/en/adonetefx/thread/cacf6a76-09a8-4c90-9502-d8b87c2f6bea

这基本上是发生在一个外键指向另一个表的主键...但是,如果我脱下 StoreGeneratedPattern 身份,然后它会尝试将值插入标识字段

It's basically happening when a Foreign key is pointed at the primary key of another table...but if I take off the StoreGeneratedPattern as Identity, then it tries to insert a value into the identity field

修改

那么,是什么这似乎是是EF4不能处理空关系时,主键被设置为 StoreGeneratedPattern =身份。如果我创建一个FK指向这个主键,并使其可为空(有效地创建一个 0 ... M 的关系),然后将它抛出这个编译错误。

So, what it seems to be is that EF4 can't handle a null relationship when the primary key is set to StoreGeneratedPattern="Identity". If I create a FK pointing to this primary key, and make it nullable (effectively creating a 0...M relationship), then it throws this compilation error.

删除 StoreGeneratedPattern =身份解决了问题,但会导致问题elseware 它的工作原理,如果外键被设置为不可空

Removing StoreGeneratedPattern="Identity" fixes the issue, but causes issues elseware It works if the foreign key is set to not nullable

推荐答案

所以,这周我采访了有人在DevWeek,我们设法找到了答案......

So, I spoke with someone at DevWeek this week, and we managed to find the answer...

基本上,如果我有一个实体可为空的外键关系,和实际的FK没有映射到一个标量属性,那么它抛出一个不稳定。如果FK不为空,那么它没有被映射到一个标量属性,它可以只在关联映射(这是我期望)。但是EF给我的选择,包括FK列生成模型时 - 而我说没有感谢你 - 所以也没添加这个标量属性...等失败的汇编。如何很奇怪!

Basically, if I have a nullable foreign key relationship on an entity, and the actual fk isn't mapped to a scalar property, then it throws a wobbly. If the fk is not null, then it doesn't have to be mapped to a scalar property, it can just be mapped in the association (which is what I'd expect). But EF gave me the choice to include FK columns when the model was generated - to which I said no thank you - so it didn't add this scalar property...and so failed the compilation. How very strange!!