繁体   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