Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Linq entity inheritance

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use Linq entity inheritance. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Linq entities inherit the use of

Once entity inheritance is defined, we can use it with Linq entity inheritance. First, customize a DataContext:

Public partial class BBSContext: DataContext {public Table BoardCategories; public Table Boards; public Table Topics; public BBSContext (string connection): base (connection) {}}

Then, let's test whether Linq to sql can recognize derived classes based on TopicType:

BBSContext ctx = new BBSContext ("server=xxx;database=BBS;uid=xxx;pwd=xxx")

Var query = from t in ctx.Topics select t

Foreach (Topic topic in query)

{

If (topic is NewTopic)

{

NewTopic newtopic = topic as NewTopic

Response.Write ("title:" + newtopic.TopicTitle + "Type:" +

Newtopic.TopicType + "")

}

Else if (topic is Reply)

{

Reply reply = topic as Reply

Response.Write ("title:" + reply.TopicTitle + "Type:" +

Reply.TopicType + "subordinate topic:" + reply.ParentTopic + "")

}

}

Then we add some data to the Topics table, as shown in the following figure:

Start the program and get the following test results:

Let's take a look at how to add and delete.

NewTopic nt = new NewTopic () {TopicTitle = "still a new theme"

TopicContent = "still new theme"}

Reply rpl = new Reply () {TopicTitle = "still new reply"

TopicContent = "still new reply", ParentTopic = 4}

Ctx.Topics.Add (nt)

Ctx.Topics.Add (rpl)

Ctx.SubmitChanges ()

Rpl = ctx.Topics.OfType () .Single (reply = > reply.TopicID = = 8)

Ctx.Topics.Remove (rpl)

Ctx.SubmitChanges ()

This is the end of this article on "how to use Linq entity inheritance". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report