简体   繁体   English

如何使用来自不同表的多个外键为表提供数据

[英]How feed a table with multiple foreign keys from different tables

so I have 3 models:所以我有3个模型:

 public class Contact
 {
    public int ContactID { get; set; }
    public string name { get; set; }
    public int SegmentID { get; set; }
    public Segment Segment { get; set; }
 }

 public class MedicalPlan
 { 
    public int MedicalPlanID { get; set; }
    public string name { get; set; }
    public int SegmentID { get; set; }
    public Segment Segment { get; set; }
 }

 public class Target 
 {
    public int TargetID { get; set; }
    public int MedicalPlanID { get; set; }
    public int ContactID { get; set; }
    public MedicalPlan MedicalPlan { get; set; }
    public Contact Contact { get; set; }
 }

A MedicalPlan got many Contacts, and Target got both many MedicalPlans and Contacts,一个 MedicalPlan 有很多联系人,Target 有很多 MedicalPlan 和联系人,

Now Each MedicalPlan has a buttom called generate: Example现在每个 MedicalPlan 都有一个名为 generate 的按钮:示例

What I want is when you press that buttom it creates a Target and generates every Contacts that are associated to that MedicalPlan through SegmentID and insert them in the table Target as shown here我想要的是当您按下该按钮时,它会创建一个目标并生成通过 SegmentID 与该 MedicalPlan 关联的每个联系人,并将它们插入表目标中,如下所示

I've tried something like this:我试过这样的事情:

                    IEnumurable<int> cons =
                        from contact in contacts
                        where contact.SegmentID == planMedical.SegmentID
                        select contact.ContactID;

                        int[] res = cons.ToArray();
                       


                        for ( int j = 0; j < res.Length ; j++)
                        {
                            targets.PlanMedicalID = id; //id MedicalPlans current row's key
                            targets.ContactID = res[j];
                            _context.Add(targets);
                            await _context.SaveChangesAsync();
                        }

But it does nothing..但它什么也没做..

You are creating you ViewModel which is PlanTargets , but you have to create you Database Model Entity, you have to create the object as:您正在创建ViewModel ,即PlanTargets ,但您必须创建Database Model 实体,您必须将 object 创建为:

for ( int j = 0; j < res.Length ; j++)
{
    var target = new Target //ADD THIS LINE
    {
        MedicalPlanID = id,
        ContactID = res[j] ​
   ​ };
    _context.Target.Add(target);
    _context.SaveChangesAsync();
}

You are not creating a Target object, you are creating a PlanTargets object and trying to add it to your DbContext.您不是在创建Target object,而是在创建PlanTargets object 并尝试将其添加到 DbContext。

NOTE: In your scenerio, you want to create a Target object for every ContactID , so in your for loop you have to create the object with the new keyword, and set the related properties, after that you have to add it to your DbContext, and when you SaveChanges then it will save the results to your database.注意:在您的场景中,您想为每个ContactID创建一个Target object ,因此在您的for loop中,您必须使用new关键字创建 object 并设置相关属性,之后您必须将其添加到您的 DbContext 中,当您SaveChanges时,它会将结果保存到您的数据库中。

暂无
暂无

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

相关问题 实体框架,带有2个外键的表,指向2个不同的表 - Entity Framework, table with 2 foreign keys to 2 different tables 不同表的两个主键作为另一个表的外键 - Two Primary keys of different tables as foreign keys to another table 实体框架6表具有许多外键,每个外键指向不同的表 - Entity Framework 6 table with many foreign keys each pointing to different tables 如何从sql多个表生成c#类(与外键相关的表) - how to generate c# class from sql multiple tables (related tables with foreign keys) 多个表的多个外键 - Multiple foreign keys to many tables 如何将两个表中的两个外键转发到第三个表中? ASP.NET 内核和 C# - How to forward two foreign keys from two tables into third table? ASP.NET Core and C# MVC实体框架-来自一个字段中多个表的外键 - MVC Entity Framework - Foreign Keys from multiple tables in one field 如何使用Entity Framework从通过外键链接的多个表中获取所有数据? - How to get all data from multiple tables linked by foreign keys with Entity Framework? 如果table1包含fk到table2的同一列的多个字段,如何从一个表创建多个外键到另一个表 - How to create multiple foreign keys from one table to another if table1 contains multiple fields with fk to the same column of table2 无法从其他外部表(2个表)将SQL数据获取到列表视图中 - Cant get sql data into listview from a different foreign table (2 tables)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM