Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 18-Oct-13 17:17pm    
Why? Don't you think you are going to violate very important Single Source of Truth principle?
—SA

1 solution

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.

C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900