Question

I'm quite new using OpenAccess so please bear with me.

I have a table called Messages which has a column called MessageTypeID, the available IDs are in a table called MessageTypes, how can I programmatically obtain the ID for a specific MessageType and assign it to the new Message object I'm creating.

Was it helpful?

Solution

There are two possible solutions for getting an existing MessageType object associated with your new Message one - please find them below:

1) Associate them directly with the whole object using its navigation property which is the recommended approach - please find an example below:

using (EntitiesModel db = new EntitiesModel())
{
    Message message = new Message();
    // Get an existing MessageType from the database e.g. the first one or
    // something like db.MessageTypes.First(mt => mt.Name == "theNameYouAreLookingFor");
    MessageType messageType = db.MessageTypes.First(); 
    message.MessageType = messageType;

    db.Add(message);
    db.SaveChanges();
}

2) Associate them using the Id of the existing object like below:

using (EntitiesModel db = new EntitiesModel())
{

    Message message = new Message();
    int messageTypeId = db.MessageTypes.First().Id;
    message.MessageTypeID = messageTypeId;

    db.Add(message);
    db.SaveChanges();
}

You could find the recommended approaches for the CRUD operations as the one that you have described at the related documentation section.

In order to get more familiar with the Telerik OpenAccess ORM you could also take a look at their Getting Started section and download the OpenAccess ORM Samples Kit containing a lot of end-to-end sample applications both on C# and Visual Basic demonstrating its integration with different scenarios like in N-Tier applications and a plenty of technologies like ASP.NET, ASP.NET MVC, ASP.NET Web API services, WCF Services, WPF, Silverlight, HTML5 and other examples about the recommended approaches for the CRUD operations, data streaming, working with stored procedures and functions and many others.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top