Skip to content

Instantly share code, notes, and snippets.

@shawnweisfeld
Created March 2, 2016 03:39
Show Gist options
  • Save shawnweisfeld/aff0968605437e5172ab to your computer and use it in GitHub Desktop.
Save shawnweisfeld/aff0968605437e5172ab to your computer and use it in GitHub Desktop.
setting a child page
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFUnary
{
class Program
{
static void Main(string[] args)
{
using (var ctx = new AppContext())
{
var page = new Page()
{
Name = "Child4"
};
page.ParentPage = ctx.Pages.FirstOrDefault(x => x.Name == "blah");
ctx.Pages.Add(page);
ctx.SaveChanges();
var child = ctx.Pages.FirstOrDefault(x => x.Name == "Child4");
Console.WriteLine(child.ParentPage.Name);
Console.ReadKey();
}
}
}
public class AppContext : DbContext
{
public AppContext()
: base (@"Server=.;Database=UnaryTest;Trusted_Connection=True;")
{
}
public DbSet<Page> Pages { get; set; }
}
public class Page
{
public int PageID { get; set; }
public string Name { get; set; }
public Page ParentPage { get; set; }
[InverseProperty("ParentPage")]
public virtual ICollection<Page> SubPages { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment