注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Lucifer 的博客

网易英超评论专栏

 
 
 

日志

 
 
 
 

how to do entity composition in LINQ TO SQL  

2007-12-18 17:10:06|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

one of my customers is going to create a class by Linq to SQL,  but as he has to use tables from different database, maybe in different server, so he wants his Linq to SQL class to be made up of multiple databases.

 

Based on my research, LINQ to SQL is not currently designed to allow for queries across multiple databases; the designer and the SQLMetal tool will only build you DataContext's that refer to items from a single database/catalog. 

 

You can, however, trick LINQ to SQL into doing your bidding by including fully qualified names for your tables.  Instead of simply Name="Customers" in the mapping use Name="[mydb].[dbo].[Customers]".  Using this technique you can make a DataContext that lists tables from any database/catalog as long as it is hosted by the same server.

 

For example:

 

Edit DataClasses1.designer.cs

 

    [System.Data.Linq.Mapping.DatabaseAttribute]

    public partial class DataClasses1DataContext : System.Data.Linq.DataContext

{

    ………………….

        public System.Data.Linq.Table< span style="color:#2b91af;" >Customer< Customers

        {

            get

            {

                return this.GetTable< span style="color:#2b91af;" >Customer<();

            }

        }

 

        public System.Data.Linq.Table< span style="color:#2b91af;" >SouthCustomer< SouthCustomer

        {

            get

            {

                return this.GetTable< span style="color:#2b91af;" >SouthCustomer<();

            }

     }                     

    }

 

    [Table(Name = "Northwind.dbo.Customers")]

    public partial class Customer : INotifyPropertyChanging, INotifyPropertyChanged

{

    ………………….                        

    }

 

    [Table(Name = "SouthWind.dbo.SouthCustomers")]

    public partial class SouthCustomer : INotifyPropertyChanging, INotifyPropertyChanged

{

    ………………….

}

 

In Northwind.dbo.Customers

 1.JPG

In SouthWind.dbo.SouthCustomers

 2.JPG

Then use linq

 

        static void Main(string[] args)

        {

            DataClasses1DataContext dbo = new DataClasses1DataContext();

 

            var query = from a in dbo.Customers

                        from b in dbo.SouthCustomer

                        where a.CustomerID == b.CustomerID

                        select new

                        {

                            company = a.CompanyName,

                            name = b.CustomerName };

 

            foreach (var e in query)

            {

                Console.WriteLine(e.ToString());

            }

          

        }

 

Result:

3.JPG

 

 

 

Another route to go is you could make a view in one database to see the tables in the other:

 

SELECT     CustomerID, CustomerName

FROM         SouthWind.dbo.SouthCustomers

 4.JPG

 

Then drag the view and table into O/R Designer:

 5.JPG

 

Then auto-generated in DataClasses1.designer.cs

  9.JPG

 

Then the LINQ object mapper should be able to see the view and should be able to query objects in that other database just fine through the view

 

In Northwind.dbo.Customers

 6.JPG

In Northwind.dbo.SouthCustomers(view)

7.JPG 

 

Then use linq

 

        static void Main(string[] args)

        {

            DataClasses1DataContext dbo = new DataClasses1DataContext();

 

            var query = from a in dbo.Customers

                        from b in dbo.SouthCustomer

                        where a.CustomerID == b.CustomerID

                        select new

                        {

                            company = a.CompanyName,

                            name = b.CustomerName };

 

            foreach (var e in query)

            {

                Console.WriteLine(e.ToString());

            }

          

        }

 

Result:

8.JPG

how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客how to do entity composition in LINQ TO SQL - Lucifer - Lucifer 的博客
  评论这张
 
阅读(259)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018