简体   繁体   English

实体框架与 .NET 4.0 多对多关联

[英]Entity Framework with .NET 4.0 many to many Association

I am new to .NET and EF and I'm working on an application that requires dynamic querying of the entities.我是 .NET 和 EF 的新手,我正在开发一个需要动态查询实体的应用程序。 I have a db with 3 tables:我有一个包含 3 个表的数据库:

**Employee**      **EmployeeSkills**      **Skill**
Employee_ID       Id                      Skill_ID
Name              Employee_ID             Name
Address           Skill_ID                etc.
etc.              Level

I will be sending from my client property names for selecting and dictionaries with property-value pairs for filtering, for example {"name", "skill",[{"skillId="1"},{"level=2"}]} will return all employees having the skill assoctiated with id 1 and konoledge of this skill at level 2.我将从我的客户端属性名称发送用于选择和带有属性值对的字典进行过滤,例如 {"name", "skill",[{"skillId="1"},{"level=2"}]将返回所有具有与 id 1 相关联的技能和该技能在级别 2 的 konoledge 的员工。

I really need to design my entities so that the queries are as simple as possible, meaning no joins or complex sql's.我真的需要设计我的实体,以便查询尽可能简单,这意味着没有连接或复杂的 sql。 I already managed to implement everything for selecting and filtering properties but I'm still struggling with many-to-many relationships.我已经设法实现了用于选择和过滤属性的所有内容,但我仍在努力处理多对多关系。

I know that if my intermediary table would have only the Employee_ID and Skill_ID fields then i could just reference the skills using Employee.Skills.Is there any way to to this using my current table structure?我知道如果我的中间表只有 Employee_ID 和 Skill_ID 字段,那么我可以使用 Employee.Skills 引用技能。有没有办法使用我当前的表结构来解决这个问题?

I'm hoping there is some kind of mapping, like defining some kind of navigation properties on the Employee entity that stores the query required to get all the Skills of an Employee with their Level.我希望有某种映射,比如在 Employee 实体上定义某种导航属性,该实体存储获取员工所有技能及其级别所需的查询。

PS I'm using VS 2012, EF runtime version 4.0.3, version 4.4.0 PS 我使用的是 VS 2012,EF 运行时版本 4.0.3,版本 4.4.0

If you use codefirst approach, you can configure your relationship with Fluent API:如果您使用 codefirst 方法,您可以配置您与 Fluent API 的关系:

HasMany(e => e.Skills).WithMany(s=>s.Employees).Map(se =>
            {
                se.MapLeftKey("Skill_ID");
                se.MapRightKey("Employee_ID");
                se.ToTable("EmployeeSkills");
            });

Update In DB-First approach, you cannot configure navigation properties in your case.更新在 DB-First 方法中,您不能在您的情况下配置导航属性。 This is link where yu can read what you can do in your situation. 这是您可以阅读在您的情况下可以做什么的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM