This is a migrated thread and some comments may be shown as answers.

RIA, adding new entities and relation between them

6 Answers 158 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vasile
Top achievements
Rank 1
Vasile asked on 09 Feb 2011, 12:03 AM

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

 

6 Answers, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 11 Feb 2011, 06:27 PM
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.
0
Vasile
Top achievements
Rank 1
answered on 12 Feb 2011, 09:18 AM
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
0
Vasile
Top achievements
Rank 1
answered on 16 Feb 2011, 10:16 AM
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
0
Damyan Bogoev
Telerik team
answered on 17 Feb 2011, 10:12 AM
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
0
Kristof
Top achievements
Rank 1
answered on 13 Jul 2011, 03:22 PM

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

0
Damyan Bogoev
Telerik team
answered on 13 Jul 2011, 03:56 PM
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!

Tags
General Discussions
Asked by
Vasile
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Vasile
Top achievements
Rank 1
Kristof
Top achievements
Rank 1
Share this question
or