In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to write DataContext in Linq to SQL". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Strongly typed DataContext
In Linq to SQL's * * article, it is said that DataContext is an entry point, and almost all of the operations we do with Linq to SQL are imposed on this class. In the code generated using VS's designer, we will see a partial class inherited from DataContext, which is accustomed to calling this class strongly typed DataContext, which further encapsulates DataContext.
Today, let's discuss in detail some places that have not been introduced to DataContext.
First, let's hand-write a strongly typed DataContext:
[Database (Name= "CnBlogs")]
Public class CnBlogsDataContext: DataContext
{
Public CnBlogsDataContext (string fileOrConnectionString)
: base (fileOrConnectionString)
{}
Public CnBlogsDataContext (string fileOrConnectionString
MappingSource mapping)
: base (fileOrConnectionString, mapping)
{}
Public CnBlogsDataContext (IDbConnection conn)
: base (conn)
{}
Public CnBlogsDataContext (IDbConnection conn, MappingSource mapping)
: base (conn, mapping)
{}
Public Table Posts
{
Get {return this.GetTable ();}
}
Public Table Blogs
{
Get {return this.GetTable ();}
}
Public Table Users
{
Get {return this.GetTable ();}
}
[Function (Name = "dbo.GetPostsByBlogId")]
Public ISingleResult GetPostsByBlogId (
[Parameter (Name= "blogid", DbType= "int")]
Int blogid)
{
IExecuteResult result = this.ExecuteMethodCall (this
((MethodInfo) (MethodInfo.GetCurrentMethod (), blogid)
Return (ISingleResult) result.ReturnValue
}
[Function (Name = "dbo.GetBblogsOrPosts")]
[ResultType (typeof (Blog))]
[ResultType (typeof (Post))]
Public IMultipleResults GetBlogsOrPosts (
[Parameter (Name = "kind", DbType = "int")]
Int kind)
{
IExecuteResult result = this.ExecuteMethodCall (this
(MethodInfo) MethodInfo.GetCurrentMethod ()), kind)
Return (IMultipleResults) result.ReturnValue
}
[Function (Name = "dbo.GetBblogsAndPosts")]
[ResultType (typeof (Blog))]
[ResultType (typeof (Post))]
Public IMultipleResults GetBlogsOrPosts ()
{
IExecuteResult result = this.ExecuteMethodCall (this
((MethodInfo) MethodInfo.GetCurrentMethod ()
Return (IMultipleResults) result.ReturnValue
}
}
There are four features in this class that I didn't see before: Database,Function,Parameter,ResultType, needless to say Database, is used for database mapping.
Linq to SQL can not only do the mapping between fields and properties, but also can map stored procedures or user-defined methods to. Net methods. Isn't that powerful? This mapping is done through Function and Parameter.
Function has two properties, IsComposable and Name,Name, which are used to specify the name of a stored procedure or user-defined method in the database. When IsComposable is true, it indicates that the method corresponds to a user-defined method, otherwise it corresponds to a stored procedure. The default is false. The Function feature can only be placed on top of a method.
Parameter is used to map the parameters accepted by a stored procedure or method.
This is the end of the content of "how to write DataContext in Linq to SQL". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.