【问题标题】:Foreign Keys wont create in SQLite database using SQLite.net外键不会使用 SQLite.net 在 SQLite 数据库中创建
【发布时间】:2016-04-15 05:58:29
【问题描述】:

我正在尝试使用 SQLite.Net 和 SQLiteNetExtensions 设置 SQLite 数据库。我似乎无法在数据库中创建外键。我已删除所有表并使用模型类重新创建它们。我已经检查了 SQLite 管理器中的编译指示并打开了外键。在我的示例中,一个地块可以有许多树。关于我可能遗漏的任何建议?

public class Tree
    {
        [PrimaryKey, AutoIncrement]
        public int TreeId { get; set; }
        [NotNull]
        public int TreeNumber { get; set; }
        [NotNull]
        public double Dbhob { get; set; }

        public double? Height { get; set; }
        [NotNull,Default(value: false)]
        public bool? HeightTree { get; set; }
        public string QualityCode { get; set; }
        [ForeignKey(typeof(Plot))]
        public int PlotId { get; set; }
        [ManyToOne]
        public Plot PlotInverse { get; set; }


    }



    public class Plot
    {
        [PrimaryKey, AutoIncrement]
        public int PlotId { get; set; }
        [NotNull, Unique]
        public System.Guid PlotGuid { get; set; }
        [NotNull, MaxLength(60)]
        public string Strata { get; set; }
        [NotNull, MaxLength(60)]
        public string PlotNumber { get; set; }

        public DateTime MeasureDate { get; set; }
        [MaxLength(70)]
        public String AssessorLead { get; set; }
        [MaxLength(60)]
        public String AssessorCrew { get; set; }
        [Default(value: 0)]
        public double PlotArea { get; set; }
        public double BasalArea { get; set; }

        [OneToMany("PlotId","PlotId")]
        public List<Tree> Trees { get; set; }
    }

【问题讨论】:

    标签: sqlite sqlite-net-extensions sqlite.net


    【解决方案1】:

    外键属性不会在 sqlite 数据库中创建,因为 sqlite-net 不支持外键,并且 SQLite-Net 扩展是在它之上构建的。它们在运行时用于保存和加载相关对象。


    作为旁注,您应该更改此行:

    [OneToMany("PlotId","PlotId")]
    public List<Tree> Trees { get; set; }
    

    到这里:

    [OneToMany("PlotId","PlotInverse")]
    public List<Tree> Trees { get; set; }
    

    或将其全部删除,让自动发现发挥作用:

    [OneToMany]
    public List<Tree> Trees { get; set; }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多