I have these two tables
public partial class Items
{
public Items()
{
this.Variables = new HashSet<Variable>();
}
public int Id { get; set; }
public string name {get; set};
//other columns
public virtual ICollection<Variable> Variables { get; set; }
}
}
And
public partial class Variable
{
public Variable()
{
this.Items= new HashSet<Items>();
}
public int Id { get; set; }
public string name {get; set};
//other stuff
public virtual ICollection<Items> Items{ get; set; }
}
}
tr These were both created automatically from a database first Entity Framework (update model from database in the edmx file). I need to be able to update the junction table that is created from
public virtual ICollection<Template> Templates { get; set; }
public virtual ICollection<Variable> Variables { get; set; }
The junction table is just two columns TemplateId and VariableID which are both the FK to the respective tables.These need to match on the name of Item and Variable. which currently i am able to accomplish somewhat with (I dont get ID from the Item Table):
foreach (var tempItem in db.Items)
{
var item = db.Variables.FirstOrDefault(x => x.Name tempItem.Name);
db.Variables.Add(item);
}
db.SaveChanges();
edit: The sql for what i am trying to accomplish is:
select Top 1000 V.ID, I.ID
FROM [PageflexWeb].[dbo].[Variables] as V, [PageflexWeb].[dbo].[Items] as I
Where V.Name = I.Name
Which works fine, but i need a way to save this to the junction table dbo.ItemVariables
Any help would be greatly appreciated.
I have read through all the documentation on msdn and stack but I am unable to find the answer that I am looking for.
Db.Templates.Add(newItem);
foreach (var record in variable.Where(record => record.Name == newItem.Name))
{
record.Templates.Add(newItem);
}
}
Db.SaveChanges();
so i needed to add the newItem to the item table - then load the records and move through them building the relationship. Unless there is another way with automatic updating (changeTracking?) I am going to mark this as the answer. Thanks @Robert McKee
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.