使用来自表A的数据和添加的字段为表B种子 [英] seed table B with data from table A with added fields

查看:125
本文介绍了使用来自表A的数据和添加的字段为表B种子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC CF EF LINQ的新手

大多数种子示例在配置文件中使用固定数据,并使用EF迁移进行更新.我需要使用表内容作为种子.

我有一张与部队有关的功绩表.该表将有100个代表各种徽章的条目
类优点徽章
public int meritbadgeID {get;设置:}
public int TroopID {get;设置:}
公共字符串徽章名{get;设置;}
公共虚拟部队Troop {get;设置;}

smeritbadge类
public int smeritbadgeID {get;设置:}
public int ScoutID {get;设置;}
公共字符串徽章名{get;设置;}
公开日期时间?开始日期{get;设置;}
公开日期时间?完成{get;设置;}
public bol inactive {get;设置;}
公开的虚拟侦察员侦察兵{get;设置;}

类似的表格smeritbadge,可针对每个侦察员跟踪每个徽章完成后的进度.

创建新的Scout时,我想找到新分配的ScoutID,然后读取meritbadge表中的100个条目,将它们与scoutID关联,然后将它们插入到smeritbadge表中.侦察兵与部队有关.

我看到了关于继承,TPH和TPT的讨论,但是我稍后会看到几个不同的meritbadge表(多维数据集侦察兵),因此我认为这种蛮力"方法对我来说将是最灵活的.

我将dbcontext scoutcontext命名为
.
我知道如何执行侦察控制器创建功能,并且此代码将被放置在RedirectToAction("Index")语句之前的创建控制器中.
db.Scouts.Add(scout);
db.SaveChanges();
此处是种子代码
返回RedirectToAction("Index");

我知道我要问的很多,但是认为对于像I这样的其他初学者来说,这可能是一个不错的教程.请尽可能明确.讨论每个陈述的内容将具有教育意义.

非常感谢John

newbie to MVC CF EF LINQ

Most of the seed examples use fixed data in the configuration file and use EF migrations to update. I need to use a table content as the seed.

I have a table of meritbadges associated with a troop. This table would have 100 entries representing the various badges
Class meritbadges
public int meritbadgeID {get; set:}
public int TroopID {get; set:}
public string badgename {get; set;}
public virtual Troop Troop {get; set;}

Class smeritbadge
public int smeritbadgeID {get; set:}
public int ScoutID {get; set;}
public string badgename {get; set;}
public datetime? startdate {get; set;}
public datetime? completedate {get; set;}
public bol inactive {get; set;}
public virtual Scout Scout {get; set;}

A similar table smeritbadge, keeps track of the progress as each badge is completed, for each scout.

When a new scout is created, I want to find the newly assigned ScoutID, then read the 100 entries in the meritbadge tables, associate them the scoutID and then insert them into table smeritbadge. Scout is associated to troop.

I see a discussion of inheritance, TPH and TPT, but I forsee several different meritbadge tables at a later time (cub scouts), so I think this "brute force" approach will be the most flexible for me.

I named my dbcontext scoutcontext.

I know how to do the scout controller create function, and that this code would be put in the create controller just before the RedirectToAction("Index") statement.
db.Scouts.Add(scout);
db.SaveChanges();
Seed code here
return RedirectToAction("Index");

I know I''m asking a lot, but think this could become a nice tutorial for other beginners such as I. Please be as explicit as possible. Discussion of what each statement does would be educational.

Many thanks John

推荐答案

与许多事情一样,一旦您弄清楚了,这很简单.注意,步骤是功绩徽章,任务是功勋徽章,很多是侦察兵.

As with many things once you figure it out it''s pretty simple this works. Note that steps are meritbadges, tasks are smeritbadges and lot is a scout.

int lotkey = lot.LotID;   //find the ID of the newly created lot

        //and open the task table so we may add data
        using (var thisdb = new HouseContext())      //open a new context to be able to read and copy
        {
            foreach (var mystep in thisdb.Steps)
            {
                Task Task = new Task();
                Task.LotID = lotkey;           // these 2 uniquely identify task collection
                Task.BuilderID = mystep.BuilderID;

                Task.StepName = mystep.StepName;
                Task.Stepseq = mystep.Stepseq;
                Task.Draw = mystep.Draw;
                Task.Inactive = false;
                thisdb.Tasks.Add(Task);
            }
                try
                {
                    thisdb.SaveChanges();



我希望这对其他人有帮助.



I hope this helps someone else.


这篇关于使用来自表A的数据和添加的字段为表B种子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆