컨텍스트 : 우리는 SQLite-Net Extensions
로컬 데이터 캐싱에 Xamarin
. iOS, Android 및 Windows Phone에 배포 할 계획입니다. 이러한 방식으로 저장하려는 시스템 전체 (모두 공통 인터페이스 구현)에 사용 된 기존 데이터 구조가 있습니다.
문제 코드 샘플에서 볼 수 있듯이 [ManyToOne]
속성은 관계 필드를 나타내는 데 사용됩니다. 작동하지 않습니다. 온 바와 같이 의 Bitbucket 개발자 페이지[ForeignKey]
속성은 외래 키 관계를 지정하는 데 사용할 수 있습니다. 이것은 int
. Id 필드의 속성을 복제하지 않고 이러한 관계를 지원하도록 구조를 쉽게 조정할 수 있습니까? 예를 들어 다음은 바람직하지 않습니다.
[ForeignKey(typeof(Address))]
public int AddressId { set; get; }
[ManyToOne]
public Address Address
{
set { address = value; }
get { return address; }
}
코드 샘플
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
namespace Data
{
[Table("Client")]
public class Client : IData
{
private int id = -1;
private Address address = null;
public Client() { }
public Client(int id)
{
this.id = id;
}
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id
{
set { id = value; }
get { return id; }
}
[ManyToOne]
public Address Address
{
set { address = value; }
get { return address; }
}
}
[Table("Address")]
public class Address : IIdentifiable
{
private int id = -1;
private string someFields = "";
public Address() { }
public Address(int id)
{
this.id = id;
}
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id
{
set { id = value; }
get { return id; }
}
public string SomeFields
{
set { someFields = value; }
get { return someFields; }
}
}
}
SQLite-Net Extensions는 SQLite-Net보다 얇은 계층이며 저장소로 sqlite 데이터베이스를 사용합니다. 관계형 데이터베이스는 외래 키를 사용하여 관계를 저장하며 sqlite는이 측면에서 다르지 않습니다. 따라서 SQLite-Net 및 SQLite-Net Extensions는 관계 선언을 위해 외래 키 메커니즘도 사용합니다.
대안으로, ManyToMany
관계가 작동 하는 것과 같은 방식으로 중간 테이블을 사용하여 관계를 저장할 수 있지만 끝 중 하나를 하나로 제한 할 수 있습니다 . 이렇게하면 ManyToMany 관계 및 중간 테이블을 사용하여 OneToMany
, ManyToOne
또는 OneToOne
관계를 모방 할 수 있습니다. 예를 들면 :
[Table("Client")]
public class Client {
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
[Ignore] // This property shouldn't be persisted
public Address Address { get; set; }
// This relationship is in fact a ManyToOne relationship,
// but we model it as a ManyToMany to avoid adding foreign key to this entity
[ManyToMany(typeof(AddressesClients))]
public Address[] Addresses {
get { return Address != null ? new []{ Address } : Address; }
set { Address = value.FirstOrDefault(); }
}
}
[Table("Address")]
public class Address
{
[PrimaryKey, AutoIncrement, Column("_id")]
public int Id { get; set; }
public string SomeFields { get; set; }
[ManyToMany(typeof(AddressesClients), ReadOnly = true)]
public List<Client> Clients { get; set; }
}
// Intermediate table that defines the relationship between Address and Client
class AddressesClients {
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
[ForeignKey(typeof(Client))]
public int ClientId { get; set; }
[ForeignKey(typeof(Address))]
public int AddressId { get; set; }
}
물론 이것은 약간의 성능 저하를 가져옵니다.
의 PrimaryKey
경우 지원되는 모든 유형을 사용할 수 있으며 반대의 ForeignKey
경우 정확히 동일한 유형을 사용해야합니다 . 즉 Guid
, 기본 키로 사용 하는 경우 해당 클래스 를 가리키는 외래 키 Guid
도 이어야합니다 . 데모 프로젝트에서 우리는 이미 사용하고있는 int
(가장 확대됨)을, string
심지어 UUID
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다