Windows Phone数据库中的多对多关系

时间:2013-09-11 11:54:10

标签: c# windows-phone-7 linq-to-sql windows-phone

我一直在网上搜索,但找不到加速在WindowsPhone模拟器上创建数据库的解决方案。我的问题是:有没有办法加快跟随多对多的关系?为了创建数据库,我必须为每个SimilarCollection添加一个Word的列表,并且在每个{I}}中我必须添加一些WordJunctionSimilar,它应该存储对某些Word的引用。这令人沮丧,而且非常复杂。有更好的解决方案吗?

(只有SimilarCollectionWordJunctionSimilar代码很重要)

[Table]
public class Word
{
    [Table]
    public class SimilarCollection
    {
        [Column(IsPrimaryKey = true)]
        public int SimilarId { get; set; }
        [Column]
        public string Translation { get; set; }

        [Column]
        public int? WordId { get; set; }
        private EntitySet<WordJunctionSimilar> junction;
        [Association(Name = "FK_Similar_Words1", Storage = "junction", ThisKey = "SimilarId", OtherKey = "SimilarId")]
        public EntitySet<WordJunctionSimilar> Junction
        {
            get { return junction; }
            set { junction = value; }
        }
        public SimilarCollection()
        {
            Junction = new EntitySet<WordJunctionSimilar>((w) => w.SimilarId = this.SimilarId,(w) => w.SimilarId = 0);
        }

    }

    [Column(IsPrimaryKey = true)]
    public int WordId { get; set; }
    [Column(CanBeNull = false)]
    public string Foreign { get; set; }
    [Column]
    public string Conjugation { get; set; }
    [Column]
    public string SpeechPart { get; set; }
    [Column]
    public string Type { get; set; }
    private EntitySet<SimilarCollection> similar;
    [Association(Name = "FK_Words_Similar", ThisKey = "WordId", OtherKey = "WordId", Storage = "similar")]
    public EntitySet<SimilarCollection> Similar
    {
        get { return similar; }
        set { similar = value; }
    }
    public Word()
    {
        Conjugation = "";
        SpeechPart = "";
        Type = "";
        Similar = new EntitySet<SimilarCollection>(OnSimilarAdded,OnSimilarRemoved);
    }
    private void OnSimilarAdded(SimilarCollection similar)
    {
        similar.WordId = this.WordId;
    }
    private void OnSimilarRemoved(SimilarCollection similar)
    {
        similar.WordId = null;
    }
}

[Table]
public class WordJunctionSimilar
{
    [Column(IsPrimaryKey=true)]
    public int  WordId;
    [Column(IsPrimaryKey=true)]
    public int SimilarId;

    private EntityRef<Word> wordRef;
    [Association(Name="FK_Similar_Words0", Storage="wordRef",ThisKey="WordId",OtherKey="WordId")]
    public Word WordRef
    {
        get { return wordRef.Entity; }
        set { wordRef.Entity = value; WordId = value.WordId;  }
    }

    public WordJunctionSimilar()
    {
        wordRef = new EntityRef<Word>();
    }

}

1 个答案:

答案 0 :(得分:0)

加速数据库创建的最佳方法 - 不要创建数据库。而是分发完整的.SDF文件:在您的XAP中,和/或从网上下载的GZip存档中。

通常,与模拟器相比,WP7硬件设备的工作速度要慢得多。如果您的数据库 在模拟器上运行缓慢,则它可能在物理设备上完全无法使用。

我想补充一点,关系数据库(例如SQLCE)并不是一个灵丹妙药:有时,其他数据结构可能更具性能,甚至可以达到几个数量级。这适用于内存数据和存储格式。