问题标签 [navigation-properties]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
759 浏览

entity-framework-4 - EF 导航属性循环问题

我目前有一个看起来像这样的数据库

当我使用实体框架 4.0 的设计器并添加两个表时,我得到了两个类的导航属性,即:

现在我的人员表有大约 3+ 百万条记录,而地址类型只有大约 50 条左右的记录。当我更新任何人的状态并出于某种原因调用保存更改时,需要永远进行更新;如果我在代码执行过程中暂停和调试,我最终会得到一个名为 FixupAddressStatusLookup 的方法。我不知道这里到底发生了什么,但在我看来,它就像循环遍历具有相同 addressstatustype 的 Person 记录的子集;不过,这只是我最好的猜测。如果我从设计器中的 AddressStatusLookup 类中删除 Person 导航属性,则代码会立即执行。谁能解释这里可能发生了什么?为什么我的 AddressStatusLookup 上的 Person 导航属性会导致这样的延迟?

谢谢

0 投票
2 回答
3458 浏览

.net - Windows 窗体数据绑定和属性路径:如何处理可空性?

我正在尝试在 Windows 窗体项目中使用MVVM模式实现主/细节场景(我很生气,我知道)。考虑以下视图模型:

我正在尝试使用以下代码(以及 Windows 窗体数据绑定支持的属性路径)将 DetailViewModel 的 SubProperty 绑定到 TextBox:

它完美地工作(就像 WPF !!!)......直到 SelectedDetail 为空(对于我的逻辑,SelectedDetail null 意味着在主视图中没有选择任何内容)。如果 SelectedDetail 为空,我得到一个 ArgumentNullException(参数名称:组件)。

有没有办法处理“父”属性的可空性(在导航路径内)?

Here is the exception stacktrace:

0 投票
4 回答
7118 浏览

c# - 按作为实体导航属性的字段排序 - Linq to Entity

我有一个场景,我需要在一个列上排序,该列是我的 EF 模型中的用户实体的导航属性。

实体:用户 --> 国家 1:n 关系

一个简单的 SQL 查询如下所示:

因此,我尝试使用 Linq to Entities 复制上述 SQL 查询,如下所示 - (启用了延迟加载)

但是此查询不会像上面的本地 SQL 查询那样返回按名称排序的我的国家/地区。

但是,我继续进行了更多操作并执行了以下操作:

但是在 enumeratedUser 对象上订购大约 50 条记录需要大约 50 条记录。7秒

有没有更好的方法来省略 Enumerable 并且不返回匿名类型?

谢谢

编辑

我只是忘了说 EF 提供程序是 MySQL 而不是 MS SQL。事实上,我只是在 MS SQL 中的复制数据库上尝试了相同的查询,并且查询工作正常,即国家名称的排序正确,所以看起来我除了从 MySQL 获取结果集并执行命令之外别无选择从可枚举对象的内存中

0 投票
1 回答
183 浏览

.net - EF 4 导航属性问题

假设我的 TableA 有 2 个对 TableB 的关系引用,其中 TableB 是主键(假设它有一个 PrimaryBKey 和 SecondaryBKey 列作为 TableA 中的 FK 名称)。所以我有实体:

是否有任何关于 EF 如何派生命名方案的文档,并且可以更改吗?我有开发人员告诉我,当他们重新生成一个表时,它会改变关系的顺序。我的意思是,最初实体是以这种方式映射的:

但他们说它后来被切换到:

这甚至可能吗?我找不到任何有关命名方案的文档来验证这一点。

谢谢。

0 投票
1 回答
644 浏览

silverlight - 如何使用复合键更新实体的导航属性?

我在为具有复合主键的实体使用 WCF 数据服务时遇到问题。我正在使用 Silverlight 4 业务应用程序模板,并针对相关表的数据库创建了实体数据模型 (EDM),并在 Web 项目中为此 EDM 创建了 WCF 数据服务。Silverlight 项目具有对 WCF 数据服务的服务引用。到目前为止所有相当标准的东西:)

为了简单和易于调试,我将其缩小为一个非常简单的场景,如下所示:

  • Lookup表有列IdData
  • Lookup2表有列IdData
  • MyData表有列LookupIdLookup2IdSomeDateExtraData
    • LookupId是表的外键Lookup
    • Lookup2Id是表的外键Lookup2
    • LookupId,Lookup2IdSomeDate构成表的主键MyData

CollectionViewService然后,我可以使用单独的 s将查找表中的数据加载到单独的DataServiceCollections 中。CollectionViewSource我还通过第三个将主要数据加载到另一个中DataServiceCollection。对于控件ItemsSource中的属性ComboBox,我先绑定到相关的CollectionViewSource,然后再将SelectedItem属性绑定到相关的导航属性。到目前为止,这一切都很好。

然后,我添加一个保存更改按钮,其中包含一个Click调用BeginSaveChanges. 如果我更改ExtraData字段的值,然后单击保存更改,则更改将被保存而不会出现任何问题。但是,如果我更改其中一个导航属性的值,则在BeginSaveChanges调用时会出现异常:

处理请求流时出错。设置属性“查找”的值时遇到错误。请验证该值是否正确。

如果我进一步简化 EDM,使外键不属于复合键的一部分,则可以成功保存更改,但实际应用中的复合键是数据模型的重要组成部分。

关于我做错了什么以及如何更改构成复合键一部分的导航属性的任何建议?

谢谢,德里克。

0 投票
1 回答
1134 浏览

asp.net - RadGrid 设置列导航属性

我的 RadGrid 有问题...你能告诉我如何在我的 aspx 文件中使用导航属性字段设置网格的列值吗?

我正在使用实体,并且我有一个表 SapDocuments,其中包含字段 Process 的外键到另一个表“Staging”,

此刻,我的网格的值为 null ......

>

我需要使用导航属性的值设置这两个列值......我可以像这样访问它:

  • SapDocuments.Staging.Process;
  • SapDocuments.Priorities.Priority;

    /li>

我背后的代码是这样的:

0 投票
2 回答
2035 浏览

c# - How can I set the value of a Navigation Property in the code behind?

I have an Entity called Cost, which has a required property of CostType

The Cost class has a GetNew() method which sets all the Cost's defaults:

The Lists.CostTypes is a static list which is pulled from EF at startup and used in ComboBoxes

I am having problems setting the CostType within my code, after first setting it in the GetNew() method.

For example, the following code reads an excel file, and sets the default type based on a column in the Excel file, or null if it can't find a match

My problem is, during the Save operation I get the following error:

The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.

My Add Operation looks like this:

  • If I remove the line of code that manually sets the Cost when it reads the excel file, the save works fine.
  • If I change the line of code to read Lists.Costs[2], it saves fine.
  • If I remove the line of code in GetNew() which sets the default, I get an error telling me that I violated the PK rule of CostTypes, meaning it's trying to insert the Cost Type.
  • Changing the ComboBox showing Type to something else still gives the same error.
  • After loading costs from the excel file, my regular Add/Edit forms throw the same error when I change the Type and try and save. If I don't load an excel file, they work fine.

I'm still learning Entity Framework, but so far it has been nothing but a frustration and a headache to use. Does someone know what my problem is and how I can fix it?

EDIT

Here's the info requested by Slauma. I am keeping it simple and excluding unrelated objects

  • Costs are in one table and CostTypes are in another table. In the database, the Costs.TypeId column is not allowed to be null, and is a Foreign Key to CostTypes. The Id field for both tables is auto-generated.

  • My EF model is just a generic one with the two database tables added. The only change I made to it was to rename some fields and remove the CostTypes.Costs Navigation Property.

  • The Excel file that gets imported maps most costs to their matching CostType.Name, however it IS possible that the string in the excel file doesn't match a CostType, so Lists.CostTypes.FirstOrDefault(t => t.Name == row[0].ToString()) can assign aNULLvalue to theCost.Typeproperty. That doesn't seem to be a problem though, because the form still comes up with the list of costs and their default selected items. Item's with aNULLCostType do not have an item selected in the CostTypeComboBox` and trigger a validation error that must be corrected before saving.

The code to load the CostType list is

The ApplyOrder code can be found here.

The GetList method is called from

0 投票
1 回答
227 浏览

c# - 如何从不同的上下文中保存实体上的 NavigationProperty?

我有两个数据库表,一个用于事件,一个用于 RecurrenceRules。事件有一个指向 RecurrenceRuleID 的 FK。

我想做的就是保存一个事件,它是来自新上下文的重复规则,但我似乎无法做到这一点。我错过了什么??

0 投票
2 回答
1051 浏览

lambda - EF 4.1 代码优先:如何选择具有集合属性计数的实体

我有一个以Tag导航属性(集合)命名的实体,名为Articles. 有Tag一个名为ignore-property ArticleCount,用于保存相关联Article的计数(仅在运行时和在视图中使用-被数据库忽略)。看:

如何在ONETag查询中选择所有s (或一个),对 dataBase -in entity framework 4.1 code-first 进行查询,请使用 lambda 表达式ArticleCount

0 投票
1 回答
136 浏览

entity-framework-4.1 - EF 4.1 代码优先:如何设计和映射这些实体?

我有 3 个实体:MemberAuthenticationTokenEmail

  1. 每个Member可能有很多 AuthenticationTokens
  2. 每个AuthenticationToken可能有一个或零 Email
  3. 每个都Member可能有零或一 PrimaryEmail(来自Emails表格)。真的PrimaryEmailAuthenticationTokens 的关联 之一Email

所以我有:

通过上面解释的设计,我可以添加 Member 和 AuthenticationToken,但是当我想将电子邮件附加到 Member 或 AuthenticationToken(或两者)时,我给出了这个错误:

INSERT 语句与 FOREIGN KEY 约束等冲突。

这个设计对吗???如何设计我的表(和实体)以实现我的目的?以及如何在代码优先中映射我的实体?请问你有什么想法吗?

在此处输入图像描述