2017-02-17 7 views
0


SQLite.net 및 SQLite-Net Extensions을 사용하고 있습니다. 나는 다방면의 많은 자아 관계를 만들고 싶습니다.
제 경우에는 형제가있는 자식 클래스가 있지만 형제는 여전히 자식 유형입니다.

하위 클래스Many to Many "self"- 관련 SQLite : 가능합니까?

[ManyToMany(typeof(Brotherhood), CascadeOperations = CascadeOperation.All)] 
public List<Child> Siblings_DB { get; set; } 

형제 클래스

class Brotherhood 
{ 
    [ForeignKey(typeof(Child))] 
    public int ID_child1 { get; set; } 

    [ForeignKey(typeof(Child))] 
    public int ID_child2 { get; set; } 

} 

모든 일을해야한다 : 따라서 나는 manytomany 관계를 구현하기 위해 노력했다.
그때 나는 Child를 작성하고 Siblings 목록에서 형제를 추가 할 수 있지만 내가하려고 할 때 단지 ID_child1가 업데이트 InsertOrReplaceWithChildren(Child,true) 0으로
어떤 생각을 ID_child2 숙박을 이용하여 DB에 클래스를 저장? 내가 도대체 ​​뭘 잘못하고있는 겁니까?
알렉스

+0

같은 질문을 .. 다음을 확인하십시오. http://stackoverflow.com/questions/14227468/in-sqlite-how-to-implement-a-many-to-many-relationship –

답변

0

내가 많은 자아 관계를 깨닫는 데 몇 번의 시도 끝에, 나는 같은 클래스에서 두 개의 많은 관계를 맺고 혼란스러워했다. 나는 어느 것이 "공식적인"것이 었는지 궁금했다. 결국 나는 그 두 가지가 모두 이해되었다! 형제 자매의 합은 두 목록의 합계입니다.

[ManyToMany(typeof(Brotherhood), "ChildID1", "Siblings_DB_support", CascadeOperations = CascadeOperation.All)] 
public List<Child> Siblings_DB { get; set; } 
[ManyToMany(typeof(Brotherhood), "ChildID2", "Siblings_DB", CascadeOperations = CascadeOperation.All)] 
public List<Child> Siblings_DB_support { get; set; } 

그리고 관계 테이블 : 명시 적으로 외래 키와 property 속성의 역 관계를 지정해야이 시나리오에서는

public class Brotherhood 
{ 
    [ForeignKey(typeof(Child))] 
    public int ChildID1 { get; set; } 

    [ForeignKey(typeof(Child))] 
    public int ChildID2 { get; set; } 
} 
0

데이터베이스 스키마는 먼저 수행하려는 작업과 호환되어야합니다. 의 한쪽은 각각의 관계가 Key (고유) 속성 또는 속성 세트 여야하므로 아무도 자체적으로 다 대 다 자체 관계를 가질 수 없습니다. 그러나 관련 행을 보유 할 다른 테이블을 작성한 경우, 관계를 작성하려고하는 하나의 테이블의 키 속성 [s]의 사본이 두 개 포함 된 결합 된 키로 수행 할 수 있습니다.

테이블 키 myPK라고하고라는 두 번째 테이블을 작성 포함한, 2 열의 기본 키가 함께 새로운 테이블의 PK 형성 myPK1myPK2을 속성 myM2M 말 다음의 두 관계를 형성 첫 번째 표에 myPK 열이있는 열입니다.

this을 보았습니까?

+0

내 범위는 ORM을 사용하여 원시 SQLite 언어. 모든 것이 서로 다른 두 클래스간에 많은 관계가 이루어지면 작동합니다.더욱이 나는 이미 "두 번째"테이블을 만들었습니다 : 형제애는 많은 관계를 실현해야합니다. 같은 수업을 사용하면 잘못되었습니다. 나는 thx 예제를 살펴볼 것이다. –

0

.

public class TwitterUser { 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 

    public string Name { get; set; } 

    [ManyToMany(typeof(FollowerLeaderRelationshipTable), "LeaderId", "Followers", 
     CascadeOperations = CascadeOperation.All)] 
    public List<TwitterUser> FollowingUsers { get; set; } 

    // ReadOnly is required because we're not specifying the followers manually, but want to obtain them from database 
    [ManyToMany(typeof(FollowerLeaderRelationshipTable), "FollowerId", "FollowingUsers", 
     CascadeOperations = CascadeOperation.CascadeRead, ReadOnly = true)] 
    public List<TwitterUser> Followers { get; set; } 
} 

// Intermediate class, not used directly anywhere in the code, only in ManyToMany attributes and table creation 
public class FollowerLeaderRelationshipTable { 
    public int LeaderId { get; set; } 
    public int FollowerId { get; set; } 
} 

관계 대신 "부모 - 자녀"의 "오빠 - 동생이"경우 예를 들어, 두 관계를 요약해야합니다 : 여기

public class Person { 
    [PrimaryKey, AutoIncrement] 
    public int Id { get; set; } 

    public string Name { get; set; } 

    [ManyToMany(typeof(BrothersRelationshipTable), "OldBrotherId", "YoungerBrothers", 
     CascadeOperations = CascadeOperation.All)] 
    public List<Person> OlderBrothers { get; set; } 

    [ManyToMany(typeof(BrothersRelationshipTable), "YoungBrotherId", "OlderBrothers", 
     CascadeOperations = CascadeOperation.CascadeRead, ReadOnly = true)] 
    public List<Brothers> YoungerBrothers { get; set; } 

    [Ignore] 
    publc List<TwitterUser> Brothers { 
     get { return YoungerBrothers.Concat(OlderBrothers).ToList() } 
    } 
} 

// Intermediate class, not used directly anywhere in the code, only in ManyToMany attributes and table creation 
public class BrothersRelationshipTable { 
    public int OldBrotherId { get; set; } 
    public int YoungBrotherId { get; set; } 
}