Summary:
A table may have multiple types. We can use inheritance to define them.
The Application of Object Relationships solves some complex problems between data tables and tables.
Content:
Forum topic table DBO. Topics:
Field name |
Field Type |
Can be empty |
Remarks |
Topicid |
Int |
Not null |
Identity/primary key |
Topictitle |
Varchar (50) |
Not null |
|
Topiccontent |
Varchar (max) |
Not null |
|
Parenttopic |
Int |
Null |
If the post is subject, this field is null; otherwise, it is the topic ID. |
Topictype |
Tinyint |
Not null |
0-theme stickers 1-Reply to post |
Define the object class parent class: contains some existing fields.
Then define possible child classes.
Then define the features of each class and field.
Use [inheritancemapping (...)]Judgment type
Use [column (isdiscriminator = true)]Identification basis
// Base class
[Table (name = "topics")]
[Inheritancemapping (code = 0, type = typeof (newtopic), isdefault = true)]
[Inheritancemapping (code = 1, type = typeof (reply)]
Public class topic
{
[Column (isprimarykey = true, isdbgenerated = true)]
Public int topicid {Get; set ;}
[Column]
Public String topictitle {Get; set ;}
[Column]
Public String topiccontent {Get; set ;}
[Column (isdiscriminator = true)]
Public int topictype {Get; set ;}
}
// New post, topictype = 0, and the topic field parenttopic is null, that is, it does not exist
Public class newtopic: topic
{
Public newtopic ()
{
Base. topictype = 0;
}
}
// Reply to the post. The post type is topictype = 1 and the topic field is the topic ID.
Public class reply: topic
{
Public reply ()
{
Base. topictype = 1;
}
[Column]
Public int parenttopic {Get; set ;}
}
After defining the object class, the following describes how to use it. First, define datacontext:
// Define the datacontext class
Public partial class bbscontext: datacontext
{
Public table <topic> topics;
Public bbscontext (string connection): Base (connection ){}
}
Read the topic class, and then foreach determines the type row by row, and then converts the type:
Bbscontext CTX = newbbscontext ("");
VaR query = from C in CTX. Topics select C;
Foreach (topic t in query)
{
If (T is newtopic)
{
Newtopic = T asnewtopic;
// Newtopic is the newtopic type that can be used.
}
Else if (T is reply)
{
Reply reply = T asreply;
// Reply is the available reply type.
}
}
Or directly use:
Ienumerable <newtopic> TB = (from t inctx. Topics. oftype <newtopic> () Select T). tolist ();
Replyrpl = CTX. Topics. oftype <reply> (). Single (reply => reply. topicid = 8 );
Use of Object Relationships:
1. DBO. categories:
Field name |
Field Type |
Can be empty |
Remarks |
Categoryid |
Int |
Not null |
Identity/primary key |
Categoryname |
Varchar (50) |
Not null |
|
2. forum Forum table DBO. boards:
Field name |
Field Type |
Can be empty |
Remarks |
Boardid |
Int |
Not null |
Identity/primary key |
Boardname |
Varchar (50) |
Not null |
|
Boardcategory |
Int |
Not null |
Categoryid of the category table corresponding to the Forum section |
Entity class:
[Table]
Public class categories
{
Public categories ()
{
This. _ boards = newentityset <boards> ();
}
Private entityset <boards> _ boards;
[Association (thiskey = "category", storage = "_ boards")]
Public entityset <boards> boards
{
Get {return this. _ boards ;}
Set {This. _ boards. Assign (value );}
}
[Column (isprimarykey = true, isdbgenerated = true)]
Public int categoryid {Get; set ;}
[Column]
Public String categoryname {Get; set ;}
}
[Table]
Public class boards
{
[Association (thiskey = "category", storage = "_ category")]
Public categories category
{
Get {return this. _ category. entity ;}
Set
{
This. _ category. entity = value;
Value. Boards. Add (this );
}
}
Private entityref <categories> _ category;
[Column (isprimarykey = true, isdbgenerated = true)]
Public int boardid {Get; set ;}
[Column]
Public String boardname {Get; set ;}
[Column]
Public int boardcategory {Get; set ;}
}
Usage:
Response. Write ("------------- query section ------------- with 1 Category <br/> ");
VaR query1 = fromb in CTX. Boards whereb. Category. categoryid = 1 select B;
Foreach (boardb in query1)
Response. Write (B. boardid + "" + B. boardname + "<br/> ");
Response. Write ("------------- query the category with more than two sections ------------- <br/> ");
VaR query2 = fromC in CTX. boardcategories where c. Boards. Count> 2 selectc;
Foreach (boardcategoryc in query2)
Response. Write (C. categoryid + "" + C. categoryname + "" + C. Boards. Count + "<br/> ");
Usage:
Dataloadoptionsoptions = new dataloadoptions ();
Options. loadwith <boardcategory> (C => C. Boards );
CTX. loadoptions = options;
Response. Write ("------------- query the category with more than two sections ------------- <br/> ");
Varquery2 = from C inctx. boardcategories where c. Boards. Count> 2 select C;
Foreach (boardcategory C inquery2)
Response. Write (C. categoryid + "" + C. categoryname + "" + C. Boards. Count + "<br/> ");
Usage:
Boardcategory dbcat = new boardcategory () {categoryname = "Database "};
Boardoracle = new Board () {boardname = "oracle", Category = dbcat };
CTX. boardcategories. Add (dbcat );
CTX. submitchanges ();
Download source code here