我正试图让我的大脑在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; }
}
// data populator
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)
{}
}
Silverlight方面......
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;
}
答案 0 :(得分:1)
你为什么要调用ToList()? RIA返回一个继承自IEnumerable的EntitySet,因此您不需要将其放在列表中。我建议尝试一下linq语句:
using System.Linq;
Test mytest = dc.Tests.Where( x=> x.StudentAnswerID = 2).FirstorDefault();
关于编辑问题...如果您使用Entity Framework作为数据模型,请务必在创建域服务时选中“启用编辑”复选框。 CanEdit是一个只读值,告诉您是否允许编辑。
答案 1 :(得分:0)
嘿,@ johnnywhoop,你知道你可以给FirstorDefault一个谓词,对吧?意思是,而不是说:
Test mytest = dc.Tests.Where( x=> x.StudentAnswerID = 2).FirstorDefault();
你应该说:
Test mytest = dc.Tests.FirstorDefault(x=> x.StudentAnswerID == 2);
此外,您需要提供“==”运算符而不是“=”运算符。否则你只是设置它。嗯,实际上,它不会构建。