簡體   English   中英

SQLite在創建表時找不到類

[英]Sqlite cannot find class when creating table

我有兩個類, EventScoutData

public class Event
{
    [PrimaryKey]
    public int ID
    {
        get; set;
    }

    public Event()
    {
    }
    public Event(string start, string end, string name, int id, bool isCurrent)
    {
        startDate = start;
        endDate = end;
        eventName = name;
        ID = id;
        isCurrentEvent = isCurrent;
    }

    public string startDate
    {
        get; set;
    }
    public string endDate
    {
        get; set;
    }
    public string eventName
    {
        get; set;
    }

    public bool isCurrentEvent
    {
        get; set;
    }
}

public class ScoutData
{
    [PrimaryKey]
    public int ID { get; set; }   

    public ScoutData()
    {
    }

    // Some public properties, including a public Event from the other class
}

當我嘗試將表格添加到sqlite連接時

public EventDatabase()
{            
    _connection = DependencyService.Get<ISQLite>().GetConnection();
    _connection.CreateTable<ScoutData>();
    _connection.CreateTable<Event>();
}

Event表生成正常,但是ScoutData表拋出此異常:

System.NotSupportedException:不了解App6.Event

ScoutData類在ScoutData使用Event ,但是所有內容都是公共的。 我嘗試過重命名,清理等操作,但似乎無法弄清為什么sqlite將為某些類而不是其他類創建表。

就像Jason所說的,如果您使用SQLite.Net Extensions,您可以這樣做:

public class Event
{
    [PrimaryKey]
    public int Id { get; set; }

// Need the foreign key here
    [ForeignKey(typeof(ScoutData))]
    public int ScoutDataId { get; set; }

    public string startDate { get; set; }
    public string endDate { get; set; }
    public string eventName { get; set; }
    public bool isCurrentEvent { get; set; }

    public Event()
    {
    }

    public Event(string start, string end, string name, int id, bool isCurrent)
    {
        startDate = start;
        endDate = end;
        eventName = name;
        ID = id;
        isCurrentEvent = isCurrent;
    }
}

public class ScoutData
{
    [PrimaryKey]
    public int Id { get; set; } 

// I don't know what relationship you are looking for but this is one to one:
    [OneToOne(CascadeOperations = CascadeOperation.All)]
    public Event Event { get; set; }  

    public ScoutData()
    {
    }

    // Some public properties, including a public Event from the other class
}

另外,如果您使用的是異步,請在此處使用異步版本

我建議創建一個表ScoutDataEvent左右,以在兩個表之間建立連接。

public class ScoutDataEvent
{
    [PrimaryKey]
    public int ID { get; set; }

    [NotNull]
    public int EventID { get; set; }

    [NotNull]
    public int ScoutDataID { get; set; }
}

使用此表,您可以刪除ScoutData-Table中的公共事件,並使用ScoutDataID調用Event-Table。 例如 :

var sD = conn.Table<ScoutData>().FirstOrDefaultAsync().Result;
var sDE = conn.Table<ScoutDataEvent>().Where(p => p.ScoutDataID == sD.ID).FirstOrDefaultAsync().Result;
var event = conn.Table<Event>().Where(p => p.ID == sDE.EventID).FirstOrDefaultAsync().Result;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM