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 does Linq define entity relationships

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

Share

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

This article mainly introduces how Linq defines the entity relationship, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

The definition of Linq entity relationship

For example, there is a relationship between our forum classification table and the forum forum table, which is an one-to-many relationship. In other words, a forum category may have multiple forum sections, which is very common. The advantage of defining Linq entity relationships is that we can handle the conditions of relational tables without explicitly doing join operations.

First, let's take a look at the definition of the classification table:

[Table (Name = "Categories")]

Public class BoardCategory

{

[Column (Name = "CategoryID", DbType = "int identity"

IsPrimaryKey = true, IsDbGenerated = true, CanBeNull = false)]

Public int CategoryID {get; set;}

[Column (Name = "CategoryName", DbType = "varchar (50)", CanBeNull = false)]

Public string CategoryName {get; set;}

Private EntitySet _ Boards

[Association (OtherKey = "BoardCategory", Storage = "_ Boards")]

Public EntitySet Boards

{

Get {return this._Boards;}

Set {this._Boards.Assign (value);}

}

Public BoardCategory ()

{

This._Boards = new EntitySet ()

}

}

The mapping of CategoryID and CategoryName is no different, except that we have also added a Boards attribute, which returns the set of Board entities. Through the feature, we define the relational foreign key as BoardCategory (a field of the Board table). Then take a look at the entities of the 1-to-many, multi-ended block table:

[Table (Name = "Boards")]

Public class Board

{

[Column (Name = "BoardID", DbType = "int identity", IsPrimaryKey = true

IsDbGenerated = true, CanBeNull = false)]

Public int BoardID {get; set;}

[Column (Name = "BoardName", DbType = "varchar (50)", CanBeNull = false)]

Public string BoardName {get; set;}

[Column (Name = "BoardCategory", DbType = "int", CanBeNull = false)]

Public int BoardCategory {get; set;}

Private EntityRef _ Category

[Association (ThisKey = "BoardCategory", Storage = "_ Category")]

Public BoardCategory Category

{

Get {return this._Category.Entity;}

Set

{

This._Category.Entity = value

Value.Boards.Add (this)

}

}

}

Here we need to associate the classification, setting the Category property to associate using the BoardCategory field and the classification table.

The use of Linq entity relationships

Well, now we can directly associate the table in the query syntax (it is not necessary to set the foreign key relationship of the table in the database):

Response.Write ("- query blocks classified as 1 -"); var query1 = from b in ctx.Boards where b.Category.CategoryID = = 1 select b; foreach (Board b in query1) Response.Write (b.BoardID + "" + b.BoardName + "") Response.Write ("- categories with more than 2 query blocks -"); var query2 = from c in ctx.BoardCategories where c.Boards.Count > 2 select c; foreach (BoardCategory c in query2) Response.Write (c.CategoryID + "" + c.CategoryName + "" + c.Boards.Count + "") Thank you for reading this article carefully. I hope the article "how Linq defines entity relations" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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