Sqlite cannot find class when creating table

问题: I have two classes, Event and ScoutData public class Event { [PrimaryKey] public int ID { get; set; } public Event() { } public E...

问题:

I have two classes, Event and ScoutData

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
}

When I try to add tables to the sqlite connection

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

The Event table generates fine, but the ScoutData table throws this exception:

System.NotSupportedException: Don't know about App6.Event

Class ScoutData uses Event inside of it, but everything is public. I've tried renaming, cleaning, etc but can't seem to figure out why sqlite will create tables for some classes but not others.


回答1:

Like Jason said, if you use SQLite.Net Extensions you can do it like so :

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
}

Also if you are using async then use the async version here


回答2:

I would suggest to create a table ScoutDataEvent or so, to get a connection between the two tables.

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

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

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

With this Table you can delete your public Event in the ScoutData-Table and call the Event-Table with the ScoutDataID. For Example :

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;
  • 发表于 2019-03-08 20:51
  • 阅读 ( 186 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除