none
EDM. Updaiting ObjectSets RRS feed

  • Question

  • Hello, All!

    In an EDM VB project I try to change some values in a database table. I get an error "Property is ReadOnly".

    This is the code:

    Dim DBContext As New mgbtiEntities
    Dim ZeroAttrs = From AttrTbl In DBContext.TAttrs _
                            Join ObjTbl In DBContext.TObjects On AttrTbl.F_OBJID Equals ObjTbl.F_OBJID _
                            Where AttrTbl.F_ATTRDEFID = 826212 And ObjTbl.F_OBJTYPEID = 1336 And AttrTbl.F_INT64VAL Is Nothing _
                            Select AttrTbl.F_OBJID, AttrTbl.F_INT64VAL
            ZeroAttrs.ToList()
            For Each attr_ In ZeroAttrs
                Dim srcAttr = (From srcAttrTbl In DBContext.TAttrs _
                              Join srcObjTbl In DBContext.TObjects On srcAttrTbl.F_OBJID Equals srcObjTbl.F_OBJID _
                              Where srcAttrTbl.F_ATTRDEFID = 826215 And srcAttrTbl.F_OBJID = attr_.F_OBJID _
                              Select srcAttrTbl.F_INT64VAL).First
                attr_.F_INT64VAL = srcAttr.F_INT64VAL
            Next
            DBContext.SaveChanges()

    The bold string is the one which generates the error.

    I guess it is because my ObjectSets marked as ReadOnly by VB:

     Public ReadOnly Property TAttrs() As ObjectSet(Of TAttr)

    and though fields themselves are not ReadOnly, an attempt to change them generates the error.

    What can I do to resolve the problem?

    Monday, October 3, 2016 9:44 AM

Answers

  •  but I do not see how I can fix it.


    I have seen it! -)

     Dim ZeroAttrs = From AttrTbl In DBContext.TAttrs _
                            Join ObjTbl In DBContext.TObjects On AttrTbl.F_OBJID Equals ObjTbl.F_OBJID _
                            Where AttrTbl.F_ATTRDEFID = 826212 And ObjTbl.F_OBJTYPEID = 1336 And AttrTbl.F_INT64VAL Is Nothing _
                            Select AttrTbl Distinct

    This solves the problem with anonymous types completely!

    Thanks everyone!

    • Marked as answer by Shakin Aleksey Wednesday, October 5, 2016 1:31 PM
    Wednesday, October 5, 2016 1:30 PM

All replies

  • It could be that you are producing Anonymous Type objects that the properties are read only. To correct the issue if in fact you are creating Anonymous Type read only objects, then you must create a concrete object, use Linq projection and project the results into the concrete object that has properties with getter/setter.

    https://msdn.microsoft.com/en-us/library/bb384767.aspx?f=255&MSPPError=-2147217396

    Monday, October 3, 2016 2:03 PM
  • Hi Shakin Aleksey,

    In Visual Basic the projection of a query comes in two flavors one which is read-only and the other is read/write. Since you use a read only Anonymous type. We could implement the return type as a read/write by using 'Select New With' to avoid it. like this:

           Dim ZeroAttrs = (From AttrTbl In DBContext.TAttrs
                                 Join ObjTbl In DBContext.TObjects On AttrTbl.F_OBJID Equals ObjTbl.F_OBJID
                                 Where AttrTbl.F_ATTRDEFID = 826212 And ObjTbl.F_OBJTYPEID = 1336 And AttrTbl.F_INT64VAL Is Nothing
      Select New With {AttrTbl.F_OBJID,
                                     AttrTbl.F_INT64VAL}).ToList()

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, October 4, 2016 1:41 AM
    Moderator
  • Yes! It fixes the error! But it does not allow to save changes made to a SQL Server database!

    DBContext.SaveChanges()

    It returns "0", when in fact 2 records were updated.

     Dim ZeroAttrs As ObjectQuery(Of TAttr) = From AttrTbl In DBContext.TAttrs _
                            Join ObjTbl In DBContext.TObjects On AttrTbl.F_OBJID Equals ObjTbl.F_OBJID _
                            Where AttrTbl.F_ATTRDEFID = 826212 And ObjTbl.F_OBJTYPEID = 1336 And AttrTbl.F_INT64VAL Is Nothing _
                            Select AttrTbl.F_OBJID, AttrTbl.F_INT64VAL

    This code does not work either. I guess because I use "Join" in the query, or may be, because I retrieve only selected fields. Anyway, it can not cast query results to "TAttr". Well, it is obvious, that it can't, but I do not see how I can fix it.

    I have not found any examples on MSDN, which would cover my case. So, please help!


    Tuesday, October 4, 2016 8:13 AM
  •  but I do not see how I can fix it.


    I have seen it! -)

     Dim ZeroAttrs = From AttrTbl In DBContext.TAttrs _
                            Join ObjTbl In DBContext.TObjects On AttrTbl.F_OBJID Equals ObjTbl.F_OBJID _
                            Where AttrTbl.F_ATTRDEFID = 826212 And ObjTbl.F_OBJTYPEID = 1336 And AttrTbl.F_INT64VAL Is Nothing _
                            Select AttrTbl Distinct

    This solves the problem with anonymous types completely!

    Thanks everyone!

    • Marked as answer by Shakin Aleksey Wednesday, October 5, 2016 1:31 PM
    Wednesday, October 5, 2016 1:30 PM