2012-04-28 2 views
1

내가 개체 간의 대다 관계를 만들기 위해 SQLite는엔티티 프레임 워크 4.3을 사용하고 있습니다. 그러나 런타임에 Group.Parameters 및 Parameter.Groups 컬렉션은 수동으로 추가 할 때까지 비어 있습니다.EF4.3 + SQLite는 다 대다 관계

엔티티는 다음과 같습니다 OnModelCreating에서

public class Group 
{ 
    public Group() 
    { 
     Parameters = new ObservableCollection<Parameter>(); 
    } 
    public Int64 Id { get; set; } 
    public string Name { get; set; } 
    public ObservableCollection<Parameter> Parameters { get; set; } 
} 

public class Parameter 
{ 
    public Parameter() 
    { 
     Groups = new ObservableCollection<Group>(); 
    } 

    public Int64 Id { get; set; } 
    public string Name { get; set; } 
    public ObservableCollection<Group> Groups { get; set; } 
} 

: 작성 테이블

modelBuilder.Entity<Group>().HasMany(g => g.Parameters).WithMany(p => p.Groups).Map(c => 
{ 
    c.MapLeftKey("GroupId"); 
    c.MapRightKey("ParameterId"); 
    c.ToTable("Groups_has_Parameters"); 
}); 

SQL :

create table if not exists Parameters 
(
    Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
    Name TEXT NOT NULL 
); 

create table if not exists Groups 
(
    Id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 
    Name TEXT NOT NULL 
); 
create table if not exists Groups_has_Parameters 
(
    GroupId INTEGER NOT NULL, 
    ParameterId INTEGER NOT NULL, 
    PRIMARY KEY (GroupId, ParameterId), 

    FOREIGN KEY (GroupId) REFERENCES Groups(Id), 
    FOREIGN KEY (ParameterId) REFERENCES Parameters(Id) 
); 
+0

나는 다른 데이터가 일하는 테이블 데이터, – Alex

답변

2

지연로드를 사용하려면 탐색 속성을 가상으로 만듭니다. 예를 들면 다음과 같습니다.

public virtual ObservableCollection<Parameter> Parameters { get; set; } 

이렇게하면 EF는 처음 액세스 할 때 데이터베이스에서 컬렉션을 자동으로로드합니다.

당신은 당신이 명시 적으로 같은 것을 사용하여 언제든지 컬렉션을로드 할 수 있습니다 그들이 가상하거나 게으른 로딩을하지 않으려면 :

context.Entry(group).Collection(g => g.Parameters).Load(); 

또는 가가가 제안, 당신이 간절히을로드 할 수 있습니다 당신이 Inlcude를 사용하여 데이터베이스의 초기 쿼리를 할 때 컬렉션 :

context.Groups.Include(g => g.Parameters); 
0

당신은 내가 확실하지 않다 (확인 작업을해야 무엇을 가지고 SqlLite 공급자 사양),

그냥 .Include(x=>x.Parameters) 같은 검색어에 추가하십시오.

db.Groups.Include(x=>x.Parameters) 

그렇지 않으면 '게으른'입니다.