问题标签 [ef-fluent-api]

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 投票
2 回答
594 浏览

entity-framework - 如何在 Entity Framework Code First 中实现 1..n 双向关系

在试图弄清楚如何使用 Code First 在实体框架中实现双向 1..n 关系时,我感到非常困惑。例如,一个团队(由一个 Team 实体表示)有一个教练和一个经理(都由一个 Person 实体表示)。所以,我的团队模型可能如下:

我可以通过实现 Person Entity 来实现单向导航,如下所示:

和流畅的 API:

但是,虽然这允许我从 Team 实体导航到相关的 Coach 和 Manager(两个 Person 实例),但它不允许我直接从 Coach 或 Manager 导航到相关的 Team。所以,为了实现双向导航,我修改了 Person 实体如下:

虽然构建正常,但当我尝试保存到数据库时出现以下运行时错误:

因此,为了指定实体之间的顺序,我尝试通过添加“WithRequiredPricipal”来修改流畅的 API,如下所示:

但是,当我尝试在包管理器控制台中执行“添加迁移”时,我收到以下错误:

我试图实现的目标似乎是一个简单的要求,但我已经做了很多寻找解决方案的工作,但还没有找到答案。我在流畅的 API 或注释中遗漏了什么吗?

(我不想实现解决方法,例如为教练和经理实现单独的模型,因为一个团队可能有许多其他角色(例如,助理教练、公共关系经理等)。我希望每个角色都只是一个实例个人实体。)

0 投票
1 回答
730 浏览

c# - Entity framework 6.1: 1-1 and 1-many relationship in derived types and identifying relationships

Update: I should have noted that the Id property on NavigationPropertyClass is not database generated. In my actual model, NavigationPropertyClass is also part of a hierarchy scheme (using TPH) which is not shown here , and while NavigationPropertyClass has a DerivedClass1 instance and a collection of DerivedClass2 instances, this is not necessarily the case with all classes that inherit from the base class from which NavigationPropertyClass is derived.


A portion of my model looks something like this (other properties and constructors omitted for brevity):

DerivedClass1 has a 1-1 relationship with NavigationPropertyClass, while DerivedClass2 has a many-1 relationship with NavigationPropertyClass.

I'm trying to set up identifying relationships in each of the derived classes so that when the instance of NavigationPropertyClass is deleted from the database, so too will the associated instance of DerivedClass1 and any instances of DerivedClass2. The only way I can see to set this up is with TPT inheritance, but even so, I cannot get things working correctly. I'd post my Fluent API configuration, but I've tried so many permutations at this point that I wouldn't know which one to post.

Is there any way to do what I'm trying to do? If so, what does the Fluent API config look like?

0 投票
1 回答
202 浏览

c# - 具有自定义列名称的单侧配置

我很难找到正确的 Fluent 方法组合来适当地映射我的类。这基本上是我想要做的:

该类Person有一个Address HomeAddress和一个Address WorkAddress属性,但没有 ID 的整数属性。

该类Address没有其相关Person实体的任何属性。

对于这种情况,我没有在网上找到任何示例,而且我只是没有找到正确的组合。代码优先配置总是做得很好,让我感到困惑。

这是最接近我找到的示例之一,但我得到了Invalid column name 'HomeAddressID',这告诉我我可能映射了关系的错误方面。

我在正确的轨道上吗?

0 投票
1 回答
521 浏览

c# - EF 6.1 尝试建立递归关系时出错 - 自引用表和 FluentAPI

我在尝试在 Entity Framework 6.1 中创建递归关系时遇到了很大的问题。

我需要一些不同类型的类别,但已经为所有类别创建了一个“基础”抽象类。我正在使用 Table-Per-Type 分层策略。类别可能有也可能没有 ParentCategory。(如果不是,那么它是一个顶级类别)

在下面的代码中,我展示了如何创建抽象 Category 类以及 ParentCategory 和 ChildCategories 导航属性。我已将 ParentCategoryId 设为可为空,因为在顶级类别的情况下不需要它。我已经看到了一些正是我想要在这里实现的帖子,虽然我认为我已经解决了所有答案,但我仍然收到以下错误:

Category_ParentCategory::多重性与关系“Category_ParentCategory”中角色“Category_ParentCategory_Target”中的引用约束冲突。因为从属角色中的所有属性都不可为空,所以主体角色的多重性必须为“1”。

我想知道它是否与 Category 作为抽象类和我正在使用的添加的继承模型有关,因为我在其他任何关于这种递归关系的帖子中都没有看到确切的用法。任何帮助表示赞赏!

使用 FluentAPI,我已将数据库创建配置为:

0 投票
1 回答
730 浏览

c# - 使用 linq to entity 进行嵌套查询

我有以下课程:

这些ViewModels

我想编写一个linq to entity查询( )来通过它的结果是一个带有s列表的对象Fluent API来查找一个对象,并且每个都包括一个列表 ,我写了以下查询,但它有一些错误:AIdAViewModelBViewModelBViewModelCViewModels

我怎样才能做到这一点?

0 投票
1 回答
835 浏览

entity-framework - Entity Framework Navigation Property without Foreign Key Constraint

I have a code first EF Model with a simple one(PartDetails)-to-many(Parts) relationship.

While the system is offline the lookup (PartDetails) table is purged and re-populated by external job, violating the constraint.

I have tried .Map and other variants but all create the DB constraint.

How can I prevent EF from creating the DB FK constraint using the fluent API?

I understand this may not be the best architectural approach but I'm stuck with it. If I can't find a solution to do this with the Fluent API I will manually drop the constraint in a migration script or drop and re-add the constraint during the purge job.

0 投票
1 回答
460 浏览

c# - Mvc,外键关系

我使用 Mvc (Country -> State -> City) 并想使用 Fluent Api 建立外键关系,那么这里是模型。

现在我制作 UserContext 类来定义 DbContext 和 Fluent api 任何人都可以知道如何在这些实体之间建立关系

0 投票
2 回答
770 浏览

c# - 通过复杂类型属性的 HasForeignKey 关系

我有两种复杂类型:

我有一个实体类型:

这是Configuration

基本上我正在尝试通过复杂类型属性创建一个外键(并且不起作用)。

我得到的错误是:

有没有办法在直接将外键列添加到Comment实体的情况下创建关系?

0 投票
2 回答
1258 浏览

c# - EF Fluent API + 两列复合唯一性 + 自增主键

  1. 我使用流畅的 API。我不喜欢注释。

  2. 我喜欢在所有表中始终使用自动增量作为主键。

  3. 我的一些表要求两列 X 和 Y(其中 X 不是自动增量键,Y 不是自动增量键)必须是唯一的,即:不能有另一行使其具有 X1=X2 和Y1=Y2。如果我不使用自动增量键,我只需将这两个键设为键,如下所示:

    但是,正如我在 (2) 中所说,我使用的是自动增量主键

    /li>

如何在 Fluent API 中实现这种复合唯一性?

这就是我想要实现的,用 SQL 编写的:

0 投票
2 回答
69 浏览

entity-framework - 从零到一的实体框架流式 API 映射

我对 EF 很陌生(基本上刚刚开始)。我在关注时遇到问题。假设我有一个描述产品的表,该产品(基于类型)可以具有许多附加属性(出于本次查询的目的,我将其限制为两个)。

所以我们真的在谈论产品和(纸和箔类型)之间的 0-1 关系。

如何使用 fluent API 定义它?我试图使用:

……