DomainContext、Silverlight 3、代码隐藏、编辑 EntitySet

发布于 2024-08-20 10:37:08 字数 4997 浏览 7 评论 0原文

我正在尝试让我的大脑围绕着 Silverlight RIA

我已经达到了这样的程度:我可以使用一组对象创建一个对象,该对象也有一个对象集合。

包含测试问题和问题答案的测试对象。

我已经设置了关联,并且数据已发送到 silverlight 应用程序。

中的所有数据

   private void TestLoaded(LoadOperation lo)
    {
            var ce =dc.Tests.CanEdit;
            dc.Tests.ToList()[0].TestQuestions.ToList()[0].StudentAnswerID = 2;
    }

所以在我加载的回调中...我可以看到 up 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)
    {}
}

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;
    }

I'm trying to get my brain wraped around Silverlight RIA

I'm to a point where I can create an object with a collection of objects which also has a collection of objects.

Test object that holds test questions, that holds question answers.

I have the associations set up and the the data makes it to to silverlight app.

So in my loaded callback....i can see all the data in up

   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

but the next line gives the error:
This EntitySet of type 'SilverlightApplication2.Web.Question' does not support the 'Edit' operation.

So my question is why does CanEdit = true?
And what is a more graceful way of setting the value in code behind?

rest of the code.....

        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;
    }
}

Domain Service.....

 [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 side......

      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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小草泠泠 2024-08-27 10:37:08

为什么要调用 ToList()? RIA 返回一个继承自 IEnumerable 的 EntitySet,因此您不需要将其放入列表中。我建议尝试像这样的 linq 语句:

using System.Linq;
Test mytest = dc.Tests.Where( x=> x.StudentAnswerID = 2).FirstorDefault();

至于编辑问题...如果您使用实体框架作为数据模型,请务必在创建域服务时选中“启用编辑”复选框。 CanEdit 是一个只读值,告诉您是否允许编辑。

Why are you calling ToList()? RIA returns an EntitySet which inherits from IEnumerable so you shouldnt need to put it in a list. I would suggest trying a linq statement like:

using System.Linq;
Test mytest = dc.Tests.Where( x=> x.StudentAnswerID = 2).FirstorDefault();

As to the editing problem... If you are using Entity Framework as your data model be sure to check the checkbox "enable editing" when you create your domain service. CanEdit is a readonly value that tells you whether or not editing is allowed.

Bonjour°[大白 2024-08-27 10:37:08

嘿,@johnnywhoop,你知道你可以给 FirstorDefault 一个谓词,对吧?意思是,而不是说:

Test mytest = dc.Tests.Where( x=> x.StudentAnswerID = 2).FirstorDefault();

你应该说:

Test mytest = dc.Tests.FirstorDefault(x=> x.StudentAnswerID == 2);

另外,你需要给出“==”运算符而不是“=”运算符。否则你只是设置它。好吧,实际上,它不会构建。

Hey, @johnnywhoop, you know you can give the FirstorDefault a predicate, right? Meaning, instead of saying:

Test mytest = dc.Tests.Where( x=> x.StudentAnswerID = 2).FirstorDefault();

You should say:

Test mytest = dc.Tests.FirstorDefault(x=> x.StudentAnswerID == 2);

Also, you'll want to give the "==" operator and not the "=" operator. Otherwise you're just setting it. Well, actually, it wouldn't build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文