2012-08-12 4 views
0

나는 이런 일이 :EF Code first 4.3 - 목록에 항목을 추가하는 방법 <>?

public class Category 
{ 
    [Key] 
    public int Id { get; set; } 

    public string Name { get; set; } 
    public List<Thread> Threads { get; set; } 
} 
public class Thread 
{ 
    [Key] 
    public int Id { get; set; } 

    public List<Post> Posts { get; set; } 
} 

public class Post 
{ 
    [Key] 
    public int Id { get; set; } 
} 

을 그리고 난 기존 카테고리에 스레드 항목을 추가하는 방법을 모르겠어요. 나는 이와 같은 것을 시도했다 :

var context = new Db(); 
var thr = new Thread 
     {...(adding new Post item here, not important since this works perfectly)...}; 
context.Categories.Single(c => c.Name == "some_category").Threads.Add(thr); 

그러나 이것은 명백하게 작동하지 않는다.

답변

1

실제로 새 스레드 항목을 컨텍스트에 추가하여 추적합니까? 컨텍스트가 존재하는지 여부를 모르는 경우 데이터베이스에 추가하지 않습니다.

var context = new Db(); 
var thr = new Thread 
     {...(adding new Post item here, not important since this works perfectly)...}; 

//add thread to context 
context.Threads.Add(thr); 

context.Categories.Single(c => c.Name == "some_category").Threads.Add(thr); 
관련 문제