SQLite-Net 扩展两个实体之间的一对一和一对多关系 [英] SQLite-Net Extension both one-to-one and one-to-many relationships between two entities

查看:27
本文介绍了SQLite-Net 扩展两个实体之间的一对一和一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQLite-Net PCL 和 SQLite-Net 扩展来开发使用 Xamarin 的应用程序.

I am using SQLite-Net PCL together with SQLite-Net extensions for the development of an application using Xamarin.

在我的模型中,我有两个实体,我们称它们为 A 和 B,它们通过一对一和一对多关系连接.比如A和B是一对一的关系,A和B也是一对多的关系.

In my model I have two entities, let us call them A and B, that are connected by both a one-to-one and a one-to-many relationships. For instance, A has a one-to-one relationship with B, and A has also a one-to-many relationship with B.

是否可以使用 SQLite-Net 扩展来表达这种行为?

Is it possible to express such behaviour with SQLite-Net extensions?

推荐答案

可以,但是您必须在关系属性中显式声明外键和反向属性,否则库可能会为关系获取错误的外键.

Yes, but you have to explicitly declare the foreign keys and inverse properties in the relationship attribute, because otherwise the library may get the wrong foreign key for the relationship.

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

        [OneToMany("O2MClassAKey", "BObjectsInverse")]
        public List<ClassB> BObjects { get; set; }

        [OneToOne("O2OClassAKey", "BObjectInverse")]
        public ClassB BObject { get; set; }

        // Other properties
        public string Bar { get; set; }
    }
        
    public class ClassB
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [ForeignKey(typeof (ClassA))]
        public int O2MClassAKey { get; set; }

        [ForeignKey(typeof (ClassA))]
        public int O2OClassAKey { get; set; }

        // Inverse relationships, these are optional
        [ManyToOne("O2MClassAKey", "BObjects")]
        public ClassA BObjectsInverse { get; set; }
        [OneToOne("O2OClassAKey", "BObject")]
        public ClassA BObjectInverse { get; set; }

        // Other properties
        public string Foo { get; set; }
    }

请注意,OneToOne 关系的外键 O2OClassAKey 可以在任何类中声明.

Please note that the foreign key O2OClassAKey for the OneToOne relationship can be declared in any of the classes.

如果不需要反向属性,可以在relationship属性中跳过:

If you don't need inverse properties, you can skip them in the relationship attribute:

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

        [OneToMany("O2MClassAKey")]
        public List<ClassB> BObjects { get; set; }

        [OneToOne("O2OClassAKey")]
        public ClassB BObject { get; set; }

        // Other properties
        public string Bar { get; set; }
    }
        
    public class ClassB
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }

        [ForeignKey(typeof (ClassA))]
        public int O2MClassAKey { get; set; }

        [ForeignKey(typeof (ClassA))]
        public int O2OClassAKey { get; set; }

        // Other properties
        public string Foo { get; set; }
    }

这篇关于SQLite-Net 扩展两个实体之间的一对一和一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆