简体   繁体   English

使用实体框架创建两个表之间的关系

[英]Creating relationship between two tables using Entity Framework

I am working on some CRUD methods for a web app.我正在为 web 应用程序研究一些 CRUD 方法。 I am using Entity Framework with SQL Server 2008. In particular, I have two tables that are linked using a cross reference table.我正在使用带有 SQL Server 2008 的实体框架。特别是,我有两个使用交叉引用表链接的表。 It's in the following format:它采用以下格式:

  • Table Tbl_PlanTbl_Plan
    • PlanId
    • PlanNm
    • Active
  • Table Tbl_PersonTbl_Person
    • PersonId
    • PersonNm
    • PersonData
  • Xref table Xref_PersonPlan外部参照表Xref_PersonPlan
    • PersonId
    • PersonNm

Now, when I am creating a new plan I have been trying to link the two together.现在,当我创建一个新计划时,我一直试图将两者联系在一起。 I'm not sure how.我不确定如何。 I know it has something to do with referencing the relationship or creating it.我知道这与引用关系或创建关系有关。

The tables are set up correctly, because I can pull the data just fine once it's created (ie my sample entries in SQL Server) but creating the reference is where I am stumped.表格设置正确,因为我可以在创建数据后很好地提取数据(即我在 SQL 服务器中的示例条目),但创建引用是我难过的地方。 I tried the following:我尝试了以下方法:

using (Entities context = new Entities())
{
// TblPlan plan has already been instantiated
plan.TblPersons.Add(person);

context.AddToTblPlans(plan);

context.SaveChanges();

But obviously, the .Add() isn't what I'm looking for....help?但显然, .Add()不是我要找的......帮助?

The short answer is that many-to-many relationships are not supported in LINQ to SQL the same way they are in Entity Framework and other ORMs, and you'll have to implement the desired behavior yourself.简短的回答是 LINQ 到 SQL 不支持多对多关系,就像它们在实体框架和其他 ORM 中一样,您必须自己实现所需的行为。

Take a look at this article.看看这篇文章。 It describes an approach that should work for you.它描述了一种适合您的方法。

Someone just pointed me in the right direction.有人刚刚为我指出了正确的方向。 I should have waited ten more minutes.我应该再等十分钟。 I was missing the.AttachTo() - I needed to attach the reference to the context before adding, since it was throwing an InvalidArgumentException.我错过了 .AttachTo() - 我需要在添加之前将引用附加到上下文,因为它抛出了 InvalidArgumentException。

暂无
暂无

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

相关问题 使用Entity Framework 4中的复合主键在两个表之间创建关系 - Creating a relationship between two tables with a Composite Primary Key in Entity Framework 4 实体框架如何在两个表的两列之间建立关系 - Entity Framework How to have relationship between two columns of two tables 配置实体框架两个表之间的多对多关系? - Configuring Entity Framework Many to Many relationship between two tables? 如何使用实体框架在WPF c#中的两个表之间插​​入一对多关系的数据? - How to insert data for one to many relationship between two tables in WPF c# using Entity Framework? 如何使用Entity Framework从具有一对多关系的两个表中选择所有相关数据? - How to select all related data from two tables having one-to-many relationship using Entity Framework? 实体框架在抽象表和派生表之间创建不必要的关系 - Entity Framework Creates unwanted relationship between abstract and derived tables 如何指示实体框架选择两个表之间具有多个外键的正确关系? - How do I instruct Entity Framework to select the correct relationship between two tables with multiple foreign keys between them? 如何在ASP.NET实体框架中的2个表之间创建关系 - How to create relationship between 2 tables in ASP.NET Entity Framework 3个表之间的相互关系的实体框架映射 - Entity Framework mapping of 3 tables with relationship between each other 实体框架更新一对一关系的两个表 - Entity Framework updating two tables with one to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM