开发者

Is there a neat workaround to the lack of foreign key identity columns in Entity Framework 3.5?

I'm having to downgrade a site from .NET 4 beta 2 to .NET 3.5.

The biggest hurdle is all the foreign key identity values I reference/lookup, as this isn't supported natively in EF 3.5.

Does anyone know of a reasonable work-around for this?

An example of what I mean is:

contacts.Where(contact => contact.TypeGuid 开发者_运维知识库== guid)

[TypeGuid] is a FK to [ContactTypes], so I get a [ContactType] object that I can access if I use .Include("ContactType"), but not the direct ID itself.


Your sample query "just works" in EF 1 if you specify the ID property:

var someContact = Context.Contacts.Where(c => c.ContactType.Id == guid);

This is in LINQ to Entities.

In object space, you can refer to the EntityKey:

var someContact = contacts.Where(c => { var ek = c.ContactTypeReference.EntityKey;
                                        if (ek == null) return false;
                                        return ek.EntityKeyValues[0].Value.Equals(guid);
                                       });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜