开发者

EF4 exception with relationship

开发者 https://www.devze.com 2023-04-12 11:20 出处:网络
I have two entities and there are their POCO: public class DocumentColumn { public virtual long Id { get; set; }

I have two entities and there are their POCO:

public class DocumentColumn
{
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
    public virtual long? DocumentTypeId { get; set; }
}

public class DocumentType {
    public virtual long Id { get; set; }
    public virtual string Name { get; set; }
}

There is a relation between those two entities. In the db the relation called:FK_T_DOCUMENT_COLUMN_T_DOCUMENT_TYPE.

When I do:

DocumentColumns.Where(x => x.DocumentTypeId == documentTypeId).ToList();

I get the exception:

{"Metadata information for the relationship 'MyModel.FK_T_DOCUMENT_COLUMN_T_DOCUMENT_TYPE' could not be retrieved. If mapping attributes are used, make sure that the EdmRelationshipAttribute for the relationship has been defined in the assembly.  When using convention-based mapping, metadata information for relationships between detached entities cannot be determined.\r\nParameter name: relationshipName"}

I tryed to remove the relationship and the DocumentColumn table and reload them but the cod开发者_高级运维e still throws the exception.

Whet does this exception means and how can I solve it?

EDIT:

The exception happens also If I do DocumentColumns.ToList();


(Presuming you are talking about Code First ....) There is no information in either class to let CF know that there is a relationship between them. It doesn't matter that the database has the info. Entity Framework needs to have a clue about the relationship. You provide only a property with an integer. CF cannot infer a relationship. You must have something in one class or another that provides type or another. This is not a database. It's a data model. Very different things.

But that's not all. I'm guessing that this is a one to many relationship. You could either put a List property into the Document class or a Document property in the DocumentColumn class. If you only do the latter, CF and EF will NOT know about the 1:. It will presume a 1:1 (that is if you leave DocumentId integer in there, otherwise it will presume a 1:0..1). However, I think you could get away with this and then just configure the multiplicity (1:) in fluent API.

UPDATE...reading your question again, I think you are using an EDMX and designer not code first. What are you using to create your POCO classes? Are you doing code gen from the EDMX or just writing the classes. I still think the lack of a navigation property in at least ONE of the types might be the cause of the problem. The ERROR message does not suggest that...I'm only coming to this conclusion by looking at the classes and inferring my understanding of how EF works with the metadata. I could be barking up the wrong tree. FWIW, I have asked the team if they are familiar with this exception and can provide some idea of what pattern would create it. It's pretty bizarre. :)


It seems odd to me that you are using EF with a defined relationship and you are not using the related property. Can you not do:

DocumentColumns.Where(x=>x.DocumentType.Id == documentTypeId).ToList();

This is what I would expect to see in this instance.

0

精彩评论

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

关注公众号