我正在使用 Monotouch 试用 Vici CoolStorage,但似乎遇到了人际关系方面的问题。使用以下内容,我尝试使用单个调查填充数据库,然后使用 for 循环生成 5 个问题的列表。
当我运行它时,似乎存在将问题映射到数据库中的调查的问题,因此,如果有人能告诉我我错过了什么,将不胜感激!给出的错误是“Vici.CoolStorage.CSException:无法读取关系调查”。
课程
[MapTo("Survey")]
public class Survey : CSObject<Survey, int>
{
public int SurveyId { get { return (int)GetField ("SurveyId"); } }
public string Title { get { return (string)GetField ("Title"); } set { SetField ("Title", value); } }
public DateTime DateCreated { get { return (DateTime)GetField ("DateCreated"); } set { SetField ("DateCreated", value); } }
public bool IsCurrent { get { return (bool)GetField ("IsCurrent"); } set { SetField ("IsCurrent", value); } }
[OneToMany(LocalKey="SurveyId", ForeignKey="QuestionId")]
public CSList<Question> Questions { get { return (CSList<Question>)GetField("Questions"); } }
}
[MapTo("Question")]
public class Question : CSObject<Question, int>
{
public int QuestionId { get { return (int)GetField ("QuestionId"); } }
public string Text { get { return (string)GetField ("Text"); } set { SetField ("Text", value); } }
public int SurveyId { get { return (int)GetField ("SurveyId"); } }
[ManyToOne(LocalKey="QuestionId", ForeignKey="SurveyId")]
public Survey Survey { get { return (Survey)GetField ("Survey"); } set { SetField ("Survey", value); } }
}
数据库表
CSDatabase.ExecuteNonQuery (@"CREATE TABLE Survey (SurveyId INTEGER PRIMARY KEY AUTOINCREMENT, Title TEXT(100) NOT NULL, DateCreated TEXT(50) NOT NULL, IsCurrent INTEGER)");
CSDatabase.ExecuteNonQuery (@"CREATE TABLE Question (QuestionId INTEGER PRIMARY KEY AUTOINCREMENT, Text TEXT(100) NOT NULL, SurveyId INTEGER REFERENCES Survey(SurveyId))");
环形
for (int i = 1; i <= 10; i++)
{
Question q = Question.New ();
q.Text = String.Format ("Question {0}", i);
survey.Questions.Add (q);
}
survey.Save();