Silverlight RIA に頭を悩ませようとしている
オブジェクトのコレクションを持つオブジェクトのコレクションを持つオブジェクトを作成できるようになりました。
テストの質問、質問の回答を保持するテスト オブジェクト。
アソシエーションを設定し、データを Silverlight アプリに送信します。
だから私のロードされたコールバックで....私はすべてのデータを見ることができます
private void TestLoaded(LoadOperation lo)
{
var ce =dc.Tests.CanEdit;
dc.Tests.ToList()[0].TestQuestions.ToList()[0].StudentAnswerID = 2;
}
var ce =dc.Tests.CanEdit; //CanEdit = true
しかし、次の行でエラーが発生します: タイプ 'SilverlightApplication2.Web.Question' のこの EntitySet は、'編集' 操作をサポートしていません。
私の質問は、なぜ CanEdit = true なのですか? また、コードビハインドで値を設定するより適切な方法は何ですか?
コードの残りの部分.....
public class Test
{
private List<Question> _testQuestions = new List<Question>();
[Key]
public int TestID { get; set; }
public string TestName { get; set; }
[Include]
[Association("Assoc1", "TestID", "TestID,QuestionID")]
public List<Question> TestQuestions
{
get { return _testQuestions; }
set { _testQuestions = value; }
}
}
public class Question
{
private List<Answer> _questionAnswers = new List<Answer>();
[Key]
public int TestID { get; set; }
[Key]
public int QuestionID { get; set; }
public string QuestionText { get; set; }
public int CorrectAnswerID { get; set; }
public int StudentAnswerID { get; set; }
[Include]
[Association("Assoc2", "QuestionID", "QuestionID,AnswerID")]
public List<Answer> QuestionAnswers
{
get { return _questionAnswers; }
set { _questionAnswers = value; }
}
}
public class Answer
{
[Key]
public int QuestionID { get; set; }
[Key]
public int AnswerID { get; set; }
public string AnswerText { get; set; }
}
//データポピュレータ
public class TestBuilder
{
public List<Test> MakeATest()
{
var ret = new List<Test>();
var t = new Test()
{
TestID = 1,
TestName = "The Test",
};
var tq = new Question() { TestID = 1, QuestionID = 1, CorrectAnswerID=1, QuestionText = "T1Q1" };
var a = new Answer() { QuestionID = 1, AnswerID = 1, AnswerText = "T1Q1A1" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 1, AnswerID = 2, AnswerText = "T1Q1A2" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 1, AnswerID = 3, AnswerText = "T1Q1A3" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 1, AnswerID = 4, AnswerText = "T1Q1A4" };
tq.QuestionAnswers.Add(a);
t.TestQuestions.Add(tq);
//second question
tq = new Question() { TestID = 1, QuestionID = 2, CorrectAnswerID = 3, QuestionText = "T1Q2" };
a = new Answer() { QuestionID = 2, AnswerID = 1, AnswerText = "T1Q2A1" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 2, AnswerID = 2, AnswerText = "T1Q2A2" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 2, AnswerID = 3, AnswerText = "T1Q2A3" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 2, AnswerID = 4, AnswerText = "T1Q2A4" };
tq.QuestionAnswers.Add(a);
t.TestQuestions.Add(tq);
//third question
tq = new Question() { TestID = 1, QuestionID =3, CorrectAnswerID = 4, QuestionText = "T1Q3" };
a = new Answer() { QuestionID = 3, AnswerID = 1, AnswerText = "T1Q3A1" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 3, AnswerID = 2, AnswerText = "T1Q3A2" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 3, AnswerID = 3, AnswerText = "T1Q3A3" };
tq.QuestionAnswers.Add(a);
a = new Answer() { QuestionID = 3, AnswerID = 4, AnswerText = "T1Q3A4" };
tq.QuestionAnswers.Add(a);
t.TestQuestions.Add(tq);
ret.Add(t);
return ret;
}
}
ドメインサービス.....
[EnableClientAccess()]
public class TestDomainService : DomainService
{
public IEnumerable<Test> GetTest()
{
var tb = new TestBuilder();
return tb.MakeATest();
}
public void InsertTest(Test currentData)
{}
public void UpdateTest(Test currentData)
{}
public void DeleteTest(Test currentData)
{}
}
シルバーライト側……
private void GetTest_Click(object sender, RoutedEventArgs e)
{
dc.Load(dc.GetTestQuery(),
LoadBehavior.RefreshCurrent ,
TestLoaded,
null);
}
private void TestLoaded(LoadOperation lo)
{
var ce =dc.Tests.CanEdit;
dc.Tests.ToList()[0].TestQuestions.ToList()[0].StudentAnswerID = 2;
}