This article mainly describes attributes-based Linq ing of Linq to SQL. That is, ing database, ing table, ing column, ing relationship, ing stored procedure,
Ing function. However, there are three ways to create such mappings: ORD, SQLMetal, and manual code.
Create.
First, let's understand how to manually create property ing so that you can understand the code generated based on the tool. Their difference is
Tools are more efficient, but you must first design the database so that you can directly output your object class through the database structure.
1. ing Database
The ing Database is mainly specified by the Database attribute and references the namespace System. Data. Linq. Mapping;
The Code is as follows:
[Database(Name = Table<LoginUser> Table<UserBaseInfo> Table<Aritcal> MyDbContext( connection) :
Then, when creating a database connection, you can write it like this. If the database is not set, the default database name will be the MyDbContext Class Name:
MyDbContext db = MyDbContext();
In this case, the output connection SQL statement is:
[Table (Name = UserBaseInfo
3. ing Columns
The attribute used by the ing Column is Column, and many parameters are set. For details, see the following code:
//CanBeNull cannot be used in disorder. If a column named CanBeNull is set to true, but the value found from the database is null, // AutoSync: obtains the column value after the insert or update operation. The default value is never;
= , IsPrimaryKey = , IsDbGenerated = UserID { ; = , CanBeNull = , UpdateCheck = UserName { ; = , CanBeNull = , UpdateCheck = UserNickName { ; ; }
4. ing
The relationship here refers to one-to-one and one-to-one databases. The Association attribute (IsForeignKey = true) of the object field can also be used to set the primary key base table and the foreign key base table. In this way, the relationship between tables is automatically created when the database is created.
However, you need to define and represent a link in combination with EntityRef or EntitySet.
EntityRef represents a single instance in a one-to-multiple relationship. EntitySet mainly represents a set in a one-to-one and one-to-one relationship. The following code is used to describe
This section describes the relationship between the basic user information table UserBaseInfo and the Aritcal table.
1) Use of EntityRef + Association
Because EntityRef represents 1 in the 1: M relationship, it should be a member of the Aritcal object, and the generic object is UserBaseInfo
Here, the entity and Artical entity of UserBaseInfo are:
[Table(Name = = , IsPrimaryKey = , IsDbGenerated = UserID { ; = , CanBeNull = , UpdateCheck = UserName { ; EntitySet<Aritcal>= , OtherKey = EntitySet<Aritcal> { = ), Column(IsPrimaryKey = , CanBeNull = , DbType = ArticalID { ; ), Column(CanBeNull = , DbType = UserID { ; ), Column(DbType = , UpdateCheck = ArticalTitle { ; ), Column(DbType = , UpdateCheck = CategoryID { ; ), Column(DbType = , UpdateCheck = ModuleID { ; ), Column(DbType = , UpdateCheck = Content { ; EntityRef<UserBaseInfo> [Association(Name = , ThisKey = , OtherKey = , IsForeignKey = ,
Storage = { { _UserBaseInfo.Entity =
In this way, Artical automatically creates a foreign key and the generated table structure relationship is
EntitySet <Aritcal >=, OtherKey = EntitySet <Aritcal> {
See the query code: query the user information of user ID 1 in the user information table. by calling the Articals member, the system automatically associates with the user ID 1 to query the list of articles.
UserBaseInfo userbase = db.GetTable<UserBaseInfo>().Where(a => a.UserID == (Aritcal artical .Format(
The query result is as follows:
@ Password = c. Password LoginUser c. UserID =
Then add the following method to the MyDbContext class:
[Function(Name = GetPassword([Parameter(DbType = )] userid, [Parameter(DbType = )] = = .ExecuteMethodCall(= ()result.GetParameterValue(); ()result.ReturnValue; }
The call code is as follows:
+str);
The call result is as follows:
@ Password = c. Password LoginUser c. UserID =
Then write the code in vs to call
[Function(Name = , IsComposable = Fun_GetPassword([Parameter(DbType = )] = .ExecuteMethodCall( ( mypwd=+mypwd);
The call result is as follows:
Download source code
Your support is my greatest motivation. If you like it, please recommend it !!!