【问题标题】:Sqlite cannot find class when creating tableSqlite在创建表时找不到类
【发布时间】:2019-07-28 20:03:44
【问题描述】:

我有两个班级,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 类在其中使用 Event,但一切都是公开的。我尝试过重命名、清理等,但似乎无法弄清楚为什么 sqlite 会为某些类而不是其他类创建表。

【问题讨论】:

  • SQLite.NET 仅处理原始 .NET 类型。您要做的实际上是在两个表之间创建 FK 关系。有一个单独的 SQLite 扩展包可以让你做到这一点。

标签: c# android sql class xamarin


【解决方案1】:

就像 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
}

另外,如果您使用的是异步,请使用异步版本here

【讨论】:

    【解决方案2】:

    我建议创建一个表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;
    

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 2011-04-12
      • 2014-12-14
      • 1970-01-01
      相关资源
      最近更新 更多