RIA, adding new entities and relation between them

Thread is closed for posting
7 posts, 0 answers
  1. Vasile
    Vasile avatar
    33 posts
    Member since:
    Dec 2010

    Posted 08 Feb 2011 Link to this post

    Dear Telerik Team,

    I try to do something that initialy seemed to be piece of cake, but in the end I was fighting for hours with this problem without too much success. Maybe the solution is simple indeed but I fail to see it, so please help me with this.

    I wanted to experiment with something before I go deep in my project.

    I have a very simple test database including three tables
    TableA
    - Id, int, primary key, autoincrement
    - Name varchar(50)

    TableB
    - Id, int, primary key, autoincrement
    -Name varchar(50)

    TableAB - establishing many-to-many relations between the two tables above.
    -Id, int, primary key, autoincrement
    -IdA, int, foreign key in TableA
    -IdB, int, foreign key in TableB

    I created a DomainModel with the three tables, the associations are done automatically based on relations I have established in database.
    I created an OpenAccessDomainService selecting all entities and Edit capabilities for this model.

    From a Silverlight client with RIA enabled I can insert new TableA and TableB entities, I can also insert directly TableAB entities in respective entity set if I use already submitted TableA & TableB entities, these would have Ids.

    Now I was expecting that I can add relations between new entities similarily with the model supported by Linq2Sql, where on the relation entity you can just put the references to the newly created entities, event if they do not have yet the Ids allocated form database, Linq2sql will serialize the inserts and will update dinamically the Ids such way that the relation will be saved correctly with the newly allocated Foreign Keys.

    Unfortunately this experinece did not repeated in my initiative to use RIA + OpenAccess.
    On the server I have correctly generated for example

    public partial class TableAB
    {
    public virtual int IdA { get; set; }
    public virtual int IdB { get; set; }
    public virtual int Id { get; set; }
    public virtual TableA TableA { get; set; }
    public virtual TableB TableB { get; set; }
    }

    on the client, in Web.g.cs file The TableAB entity will not have entity properties, but only the int keys, Id, IdA and IdB so I cannot define the relation by specifying newly created TableA and newly created TableB because on the client only the id's of related entities are expected.

    Now based on the googling I did in the last good hours I understood that it may be possible somehow to specify some attributes in server metadataclasses to force RIA to include these properties too, but I also read that these will go only from the server to client , and they will be always null when posted back to server.

    Any ideea on solving this is appreciated.

    Thank you,
    Vasile

     

  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 11 Feb 2011 Link to this post

    Hi Vasile,

    You should add a metadata class to annotate the generated persistent type. This help article shows how to achieve that goal. Now you should place [Include] and [Association] attributes on the navigation properties within the metadata class:

    [MetadataType(typeof(ClassNameMetadata))]
    public partial class ClassName
    {
        internal sealed class ClassNameMetadata
        {
            [Include]
            [Association("OtherClassCollection", "OtherId", "ClassNameId")]
            public List<OtherClassName> OtherClassCollection { get; set; }
        }
    }

    The [Association] attribute is informing RIA that the OtherClassCollection can be reconstructed on the client by comparing the OtherId to the ClassNameId.
    The [Include] attribute automatically recreates the collection on the client.
    Hope that helps. If any other question arise do not hesitate to contact us back.

    Greetings,
    Damyan Bogoev
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. Vasile
    Vasile avatar
    33 posts
    Member since:
    Dec 2010

    Posted 12 Feb 2011 Link to this post

    Hello Damyan,

    The solution you proposed solves the problem of the entity properties appearing on client's relation class TableAB, however if I set these properties on the instance and then submit it for insertion to the server these properties will get there with null value, so the insert fails.

    I only needed these porperties on the client in the hope that I can set them with new entities which does not have yet Ids so the server will take care to serialize first the entities inserts then update the Ids in relation and then insert the relation, this is how linq2sql works.

    Not being able to do this has very big limitations, e.g in my project I have a tree of categories and I have to add a product to a category, the product is linked with a join table, the relation is many-to-many. So even if the category is not new I would have to insert the product and a relation of the new product with the category. How can be this type of behavior achieved?

    Thank you,
    Vasile
  4. Vasile
    Vasile avatar
    33 posts
    Member since:
    Dec 2010

    Posted 16 Feb 2011 Link to this post

    I have investigated how Entity Framework handles my requirement and it seems that it works as expected. Entity properties are added to Client RIA generated types, and I can establish relations not only with Ids but also with new entities.
    For now I am going to switch to Entity Framework as this limitation of OpenAccess not being able to establish relations between new entities, is too severe.

    Vasile
  5. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 17 Feb 2011 Link to this post

    Hello Vasile,

    Currently there is an issue related to the synchronization of the foreign key id for the child entity and the primary key id for the parent entity when the parent entity is not persisted to the database. We will do our best to fix it in the future.
    Actually there is a workaround that you could use. You could submit the entities to the database before trying to establish the relation between them.
    We are sorry for the inconvenience caused.

    Best wishes,
    Damyan Bogoev
    the Telerik team
  6. Kristof
    Kristof avatar
    7 posts
    Member since:
    Apr 2009

    Posted 13 Jul 2011 Link to this post

    Is there any change using associations in the OpenAccess with WCF RIA integration?
    I'm also using fetchstrategies in my WCF RIA Service but need to add alot of plumping, which could be generated by OpenAccess RLinq.
    And this for al my entities is clumbersome

    [DataContract(IsReference = true)]
        [DataServiceKey("xxID")]
        [DebuggerDisplay("xxID: {xxID}")]
        [MetadataType(typeof(xxMetadata))]
        public partial class xxxPage
        {
            internal sealed class xxMetadata
            {
                #region Constants and Fields
     
                private IList<xxxContent> _xxxContents = new List<xxxContent>();
     
                #endregion
     
                #region Public Properties
     
                [DataMember]
                public bool Confirmed { get; set; }
     
                [DataMember]
                public DateTime? CreationDate { get; set; }
     
                [DataMember]
                public int? PackagePart { get; set; }
     
                [DataMember]
                public string PageCategory { get; set; }
     
                [DataMember]
                public int? PageID { get; set; }
     
                [DataMember]
                public string PageLabel { get; set; }
     
                [DataMember]
                public int? PageNumber { get; set; }
     
                [DataMember]
                public string PagePositionType { get; set; }
     
                [DataMember]
                public string PageReference { get; set; }
     
                [DataMember]
                public int? PageTemplateID { get; set; }
     
                [DataMember]
                public long? PdfSize { get; set; }
     
                [DataMember]
                public int? PublicationID { get; set; }
     
                [DataMember]
                public int PublicationPageID { get; set; }
     
                [DataMember]
                public string PublishStatus { get; set; }
     
                [DataMember]
                public DateTime? PublishTime { get; set; }
     
                [DataMember]
                [Include]
                [Association("xxxTemplateRelation", "xxxTemplateID", "xxxTemplateID")]
                public xxxTemplate xxxTemplate { get; set; }
     
                #endregion
            }
        }

  7. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 13 Jul 2011 Link to this post

    Hi Kristof,

    Could you please provide us with more information regarding the code that we should generate? We will discuss your suggestion and implement the missing functionality in the code generation templates.
    We are looking forward to hearing from you.

    Best wishes,
    Damyan Bogoev
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Back to Top