实体框架模型首先导航属性添加无效列名查询? [英] Entity Framework Model First Navigation Properties adding invalid column names to query?

查看:127
本文介绍了实体框架模型首先导航属性添加无效列名查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的数据库中,我们有以下表格

In our database, we have the following tables

Tags
  Id (int)
  Name (string)
  IsActive (bool)
  TagType (string)

/ p>

and

DocumentStyles
  Id (int)
  Name (string)
  StyleRules (string)
  IsAvailable (bool)
  ThumbnailFileId (int nullable)
  ConceptTagId (int nullable)



使用EF 4.2设计器,我创建了相应的实体,并尝试将ConceptTagId的外键链接到标签模型。

With the EF 4.2 designer, I've created the appropriate entities, and am trying to link the foreign key of ConceptTagId to the Tag model.

当我添加关联(从Tag到DocumentStyle为0..1到许多),它正确链接外键,并将ConceptTag的导航属性添加到DocumentStyle对象。我不想在Tag对象上导航属性。

When I add the association (0..1 to many from Tag to DocumentStyle), it correctly links the foreign key and adds the navigation property of ConceptTag to the DocumentStyle object. I do not want a navigation property on the Tag object.

但是,在存储库中调用以下代码

However, when calling the following code in a repository

db.DocumentStyles.Include(d => d.ConceptTag).ToList();

生成的查询尝试访问不存在的标签表上的属性DocumentStyle_ID,也不应该它。外键是DocumentStyle表上的ConceptTagId。

the resulting query attempts to access a property DocumentStyle_ID on the Tag table, which does not exist, nor should it. The foreign key is the ConceptTagId on the DocumentStyle table.

这个id列是从哪里来的,如何摆脱它?

Where does this id column come from, and how can I get rid of it?

在相关关联的属性窗口中:

From the properties window of the relevant association:

End1多重性: ConceptTag

End2多重性:标签中的一个为零
End2 Nav属性:{NULL}(在属性中为空)

End1 Multiplicity: * of DocumentStyle
End1 Nav Property: ConceptTag
End2 Multiplicity: Zero of One of Tag
End2 Nav Property: {NULL} (its blank in the property)

推荐答案

在进一步的调查中,它违反了命名风格的惯例。为了解决这个问题,我不得不在OnModelCreating事件中实现以下规则。

Under further investigation, it comes with breaking the convention of naming styles. To resolve the issue, I had to implement in the OnModelCreating event the following rule

builder.Entity<DocumentStyle>().HasOptional(ds => ds.ConceptTag).WithMany(); 

这允许框架知道标签在关系中没有期望的互惠属性,它不尝试在以后的查询中找到DocumentStyle_Id属性

This allows the framework to know that Tag has no reciprocal property to expect in the relationship and it doesnt attempt to find the DocumentStyle_Id property in future queries

这篇关于实体框架模型首先导航属性添加无效列名查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆