Sqlite (Android):如何在另一个对象中使用对象作为属性?

问题描述 投票:0回答:1

我有一个类“课程”,我最近对其进行了更改,以将一些属性合并到另一个类“评估”中。课程的属性现在包括“公共评估 OA”和“公共评估 PA”。现在尝试初始化数据库会引发以下错误: “System.NotSupportedException:‘不了解 C971.Models.Assessment’” 我见过其他几个人因为看似完全不同的原因而犯了这个错误。关于如何纠正这个问题有什么想法吗?

或者,这是非常错误的吗?我可能期待不存在的功能。使用 FK 来引用自己表中的评估对象会更正确吗?

public class Course
{
    [PrimaryKey, AutoIncrement] public int id { get; set; }
    public int termId { get; set; }
    public DateTime startDate { get; set; }
    public DateTime endDate { get; set; }
    public DateTime dueDate { get; set; }
    public courseStatus status { get; set; } = courseStatus.planned;
    public string name { get; set; } = "Placeholder Name";
    public string notes { get; set; } = string.Empty;
    public bool alertStartDate { get; set; } = false;
    public bool alertEndDate { get; set; } = false;
    public bool isSetOA { get; set; } = false;
    public bool isSetPA { get; set; } = false;
    public Assessment OA { get; set; }
    public Assessment PA { get; set; }
    public string courseInfo { get; set; } = string.Empty;
    public string instructorName { get; set; } = string.Empty;
    public string instructorPhone { get; set; } = string.Empty;
    public string instructorEmail { get; set; } = string.Empty;
    public enum courseStatus { inProgress, completed, dropped, planned }
}
public class Assessment
{
    [PrimaryKey, AutoIncrement] public int id { get; set; }
    public string name { get; set; } = string.Empty;
    public DateTime startDate {  get; set; }
    public DateTime endDate { get; set; }
    public DateTime dueDate { get; set; }
    public string info { get; set; } = string.Empty;
    public bool startAlert { get; set; } = false;
    public bool endAlert { get; set; } = false;
}
private static SQLiteAsyncConnection? Database;
static async Task Init()
{
    Debug.WriteLine("TermTrackerDB.Init start");
    try
    {
        if (Database is not null) //Don't create DB if already exists
            return;

        Database = new SQLiteAsyncConnection(Constants.DatabasePath, Constants.Flags);
        //var result = await Database.CreateTableAsync<Course>();
        await Database.CreateTableAsync<Assessment>();
        await Database.CreateTableAsync<Course>();
        await Database.CreateTableAsync<Term>();
        
    }
    catch (Exception e)
    {
        Debug.WriteLine($"Try Catch: TermTrackerDB Init; Exception: {0}", e);
        throw;
    }
    
}

我不确定这些其他示例是否相关,但我已将它们链接到此处以了解上下文:

Xamarin.Forms Sqlite-net NotSupportedException 关于 ManyToOne 关系“不知道

SQLite 不知道类

android-sqlite maui
1个回答
0
投票

您可以尝试为

public int CourseId { get; set; }
 添加外键 ( 
Assessment.cs

)
public class Assessment
{
    [PrimaryKey, AutoIncrement] 
    public int id { get; set; }

    public string name { get; set; } = string.Empty;
    public DateTime startDate {  get; set; }
    public DateTime endDate { get; set; }
    public DateTime dueDate { get; set; }
    public string info { get; set; } = string.Empty;
    public bool startAlert { get; set; } = false;
    public bool endAlert { get; set; } = false;
     
    //add a ForeignKey
    [ForeignKey(typeof(Course))]
    public int CourseId { get; set; }
}

并为属性

[OneToOne]
OneToMany
添加
Assessment OA
(或
Assessment PA
)属性。
Course.cs

注:

1.请安装nuget

public class Course { [PrimaryKey, AutoIncrement] public int id { get; set; } public int termId { get; set; } [OneToOne] public Assessment OA { get; set; } [OneToOne] public Assessment PA { get; set; } }

2.我不知道你的数据表的关系,你可以根据你的需要调整映射关系(

SQLiteNetExtensions

,

[OneToOne]
ManyToOne`)。
3.您还可以参考此主题:

当我保存项目时,如何从一个模型访问另一个模型中的项目

© www.soinside.com 2019 - 2024. All rights reserved.