EF4异常与关系
我有两个实体,它们有 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; }
}
这两个实体之间存在关系。在数据库中,该关系称为:FK_T_DOCUMENT_COLUMN_T_DOCUMENT_TYPE
。
当我这样做时:
DocumentColumns.Where(x => x.DocumentTypeId == documentTypeId).ToList();
我得到异常:
{"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"}
我尝试删除关系和 DocumentColumn 表并重新加载它们,但代码仍然引发异常。
这个异常是什么意思,我该如何解决它?
编辑:
如果我执行 DocumentColumns.ToList();
也会发生异常
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 code 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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(假设您正在谈论“代码优先”......)
两个类中都没有信息让 CF 知道它们之间存在关系。数据库有这些信息并不重要。实体框架需要了解关系的线索。您仅提供一个带有整数的属性。 CF 无法推断出关系。您必须在一个类或另一个类中拥有提供类型或其他类型的东西。这不是数据库。这是一个数据模型。非常不同的事情。
但这还不是全部。我猜这是一对多的关系。您可以将 List 属性放入 Document 类中,也可以将 Document 属性放入 DocumentColumn 类中。如果您仅执行后者,CF 和 EF 将不知道 1:。它将假定 1:1(也就是说,如果您将 DocumentId 整数留在那里,否则它将假定 1:0..1)。但是,我认为您可以摆脱这一点,然后只需在 Fluent API 中配置多重性 (1:)。
更新...再次阅读您的问题,我认为您首先使用的是 EDMX 和设计器而不是代码。您使用什么来创建 POCO 类?您是从 EDMX 进行代码生成还是只是编写类。我仍然认为至少其中一种类型缺少导航属性可能是问题的原因。错误消息并不表明...我只是通过查看类并推断我对 EF 如何使用元数据的理解来得出此结论。我可能找错了树。 FWIW,我已询问团队他们是否熟悉此异常,并且可以提供一些有关创建此异常的模式的想法。这很奇怪。 :)
(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. :)
我觉得奇怪的是,您使用 EF 来定义关系,但没有使用相关属性。你能不这样做吗:
这是我期望在这种情况下看到的。
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:
This is what I would expect to see in this instance.