简体   繁体   English

在Telerik Open Access ORM中设置相关表的ID

[英]Setting ID from related table in Telerik Open Access ORM

I'm quite new using OpenAccess so please bear with me. 我使用OpenAccess很新,所以请耐心等待。

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. 我有一个名为Messages的表,其中有一个名为MessageTypeID的列,可用的ID位于名为MessageTypes的表中,如何以编程方式获取特定MessageType的ID并将其分配给我正在创建的新Message对象。

There are two possible solutions for getting an existing MessageType object associated with your new Message one - please find them below: 有两种可能的解决方案可以获取与您的新消息关联的现有MessageType对象 - 请在下面找到它们:

1) Associate them directly with the whole object using its navigation property which is the recommended approach - please find an example below: 1)使用导航属性直接将它们与整个对象相关联,这是推荐的方法 - 请在下面找到一个示例:

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: 2)使用现有对象的Id关联它们,如下所示:

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 . 您可以找到CRUD操作的推荐方法,就像您在相关文档部分中所描述的那样

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. 为了更熟悉Telerik OpenAccess ORM,您还可以查看其入门部分并下载包含C#和Visual Basic上许多端到端示例应用程序的OpenAccess ORM示例工具包 ,演示其与不同的场景,如N-Tier应用程序和大量技术,如ASP.NET,ASP.NET MVC,ASP.NET Web API服务,WCF服务,WPF,Silverlight,HTML5以及有关CRUD操作的推荐方法的其他示例,数据流,使用存储过程和函数以及许多其他功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM