标签:
我们使用视图和使用数据表类似,只需将视图从“服务器资源管理器/数据库资源管理器”拖动到O/R 设计器上,自动可以创建基于这些视图的实体类。我们可以同操作数据表一样来操作视图了。这里注意:O/R 设计器是一个简单的对象关系映射器,因为它仅支持 1:1 映射关系。换句话说,实体类与数据库表或视图之间只能具有 1:1 映射关系。不支持复杂映射(例如,将一个实体类映射到多个表)。但是,可以将一个实体类映射到一个联接多个相关表的视图。 下面使用NORTHWND数据库中自带的Invoices、Quarterly Orders两个视图为例,写出两个范例。
我们使用下面代码来查询出ShipCity 在London的发票。
var q = from i in db.Invoices where i.ShipCity == "London" select new { i.OrderID, i.ProductName, i.Quantity, i.CustomerName };
这里,生成的SQL语句同使用数据表类似:
SELECT [t0].[OrderID], [t0].[ProductName], [t0].[Quantity], [t0].[CustomerName] FROM [dbo].[Invoices] AS [t0] WHERE [t0].[ShipCity] = @p0 -- @p0: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
下例查询出每季的订单。
var q = from qo in db.Quarterly_Orders select qo;
生成SQL语句为:
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[City], [t0].[Country] FROM [dbo].[Quarterly Orders] AS [t0]
LINQ to SQL 支持单表映射,其整个继承层次结构存储在单个数据库表中。该表包含整个层次结构的所有可能数据列的平展联合。(联合是将两个表组合成一个表的结果,组合后的表包含任一原始表中存在的行。)每行中不适用于该行所表示的实例类型的列为 null。
单表映射策略是最简单的继承表示形式,为许多不同类别的查询提供了良好的性能特征,如果我们要在 LINQ to SQL 中实现这种映射,必须在继承层次结构的根类中指定属性 (Attribute) 和属性 (Attribute) 的属性 (Property)。我们还可以使用O/R设计器来映射继承层次结构,它自动生成了代码。
下面为了演示下面的几个例子,我们在O/R设计器内设计如下图所示的类及其继承关系。
我们学习的时候还是看看其生成的代码吧!
具体设置映射继承层次结构有如下几步:
下面是这张图生成的代码的框架(由于生成的代码太多,我删除了很多“枝叶”,仅仅保留了主要的框架用于指出其实质的东西):
[Table(Name = "dbo.Contacts")] [InheritanceMapping(Code = "Unknown", Type = typeof(Contact), IsDefault = true)] [InheritanceMapping(Code = "Employee", Type = typeof(EmployeeContact))] [InheritanceMapping(Code = "Supplier", Type = typeof(SupplierContact))] [InheritanceMapping(Code = "Customer", Type = typeof(CustomerContact))] [InheritanceMapping(Code = "Shipper", Type = typeof(ShipperContact))] public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged { [Column(Storage = "_ContactID",IsPrimaryKey = true, IsDbGenerated = true)] public int ContactID{ } [Column(Storage = "_ContactType",IsDiscriminator = true)] public string ContactType{ } } public abstract partial class FullContact : Contact{ } public partial class EmployeeContact : FullContact{ } public partial class SupplierContact : FullContact{ } public partial class CustomerContact : FullContact{ } public partial class ShipperContact : Contact{ }
日常我们经常写的形式,对单表查询。
var cons = from c in db.Contacts select c; foreach (var con in cons) { Console.WriteLine("Company name: {0}", con.CompanyName); Console.WriteLine("Phone: {0}", con.Phone); Console.WriteLine("This is a {0}", con.GetType()); }
这里我仅仅让其返回顾客的联系方式。
var cons = from c in db.Contacts.OfType<CustomerContact>() select c;
初步学习,我们还是看看生成的SQL语句,这样容易理解。在SQL语句中查询了ContactType为Customer的联系方式。
SELECT [t0].[ContactType], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address],[t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Fax],[t0].[ContactID], [t0].[CompanyName], [t0].[Phone] FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[ContactType] = @p0) AND ([t0].[ContactType] IS NOT NULL) -- @p0: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Customer]
这个例子查找一下发货人的联系方式。
var cons = from c in db.Contacts where c is ShipperContact select c;
生成的SQL语句如下:查询了ContactType为Shipper的联系方式。大致一看好像很上面的一样,其实这里查询出来的列多了很多。实际上是Contacts表的全部字段。
SELECT [t0].[ContactType], [t0].[ContactID], [t0].[CompanyName], [t0].[Phone],[t0].[HomePage], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Fax],[t0].[PhotoPath], [t0].[Photo], [t0].[Extension] FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[ContactType] = @p0) AND ([t0].[ContactType] IS NOT NULL) -- @p0: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [Shipper]
这个例子就通吃了,全部查找了一番。
var cons = from c in db.Contacts select c as FullContact;
生成SQL语句如下:查询整个Contacts表。
SELECT [t0].[ContactType], [t0].[HomePage], [t0].[ContactName], [t0].[ContactTitle],[t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Fax], [t0].[ContactID], [t0].[CompanyName], [t0].[Phone], [t0].[PhotoPath],[t0].[Photo], [t0].[Extension] FROM [dbo].[Contacts] AS [t0]
使用Case形式查找出在伦敦的顾客的联系方式。
var cons = from c in db.Contacts where c.ContactType == "Customer" && ((CustomerContact)c).City == "London" select c;
生成SQL语句如下,自己可以看懂了。
SELECT [t0].[ContactType], [t0].[ContactID], [t0].[CompanyName], [t0].[Phone], [t0].[HomePage],[t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Fax], [t0].[PhotoPath], [t0].[Photo], [t0].[Extension]FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[ContactType] = @p0) AND ([t0].[City] = @p1) -- @p0: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Customer] -- @p1: Input NVarChar (Size = 6; Prec = 0; Scale = 0) [London]
当插入一条记录时,使用默认的映射关系了,但是在查询时,使用继承的关系了。具体看看生成的SQL语句就直截了当了。
//插入一条数据默认使用正常的映射关系 Contact contact = new Contact() { ContactType = null, CompanyName = "Unknown Company", Phone = "333-444-5555" }; db.Contacts.InsertOnSubmit(contact); db.SubmitChanges(); //查询一条数据默认使用继承映射关系 var con = (from c in db.Contacts where c.CompanyName == "Unknown Company" && c.Phone == "333-444-5555" select c).First();
生成SQL语句如下:
INSERT INTO [dbo].[Contacts]([ContactType], [CompanyName], [Phone]) VALUES (@p0, @p1, @p2) SELECT TOP (1) [t0].[ContactType], [t0].[ContactID], [t0].[CompanyName], [t0].[Phone],[t0].[HomePage], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City],[t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Fax], [t0].[PhotoPath],[t0].[Photo], [t0].[Extension] FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[CompanyName] = @p0) AND ([t0].[Phone] = @p1) -- @p0: Input NVarChar (Size = 15; Prec = 0; Scale = 0) [Unknown Company] -- @p1: Input NVarChar (Size = 12; Prec = 0; Scale = 0) [333-444-5555]
这个例子说明如何插入发货人的联系方式的一条记录。
//1.在插入之前查询一下,没有数据 var ShipperContacts = from sc in db.Contacts.OfType<ShipperContact>() where sc.CompanyName == "Northwind Shipper" select sc; //2.插入数据 ShipperContact nsc = new ShipperContact() { CompanyName = "Northwind Shipper", Phone = "(123)-456-7890" }; db.Contacts.InsertOnSubmit(nsc); db.SubmitChanges(); //3.查询数据,有一条记录 ShipperContacts = from sc in db.Contacts.OfType<ShipperContact>() where sc.CompanyName == "Northwind Shipper" select sc; //4.删除记录 db.Contacts.DeleteOnSubmit(nsc); db.SubmitChanges();
生成SQL语句如下:
SELECT COUNT(*) AS [value] FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[CompanyName] = @p0) AND ([t0].[ContactType] = @p1) AND ([t0].[ContactType] IS NOT NULL) -- @p0: Input NVarChar [Northwind Shipper] -- @p1: Input NVarChar [Shipper] INSERT INTO [dbo].[Contacts]([ContactType], [CompanyName], [Phone]) VALUES (@p0, @p1, @p2) -- @p0: Input NVarChar [Shipper] -- @p1: Input NVarChar [Northwind Shipper] -- @p2: Input NVarChar [(123)-456-7890] SELECT COUNT(*) AS [value] FROM [dbo].[Contacts] AS [t0] WHERE ([t0].[CompanyName] = @p0) AND ([t0].[ContactType] = @p1) AND ([t0].[ContactType] IS NOT NULL) -- @p0: Input NVarChar [Northwind Shipper] -- @p1: Input NVarChar [Shipper] DELETE FROM [dbo].[Contacts] WHERE ([ContactID] = @p0) AND ([ContactType] = @p1) AND ([CompanyName] = @p2) AND ([Phone] = @p3) -- @p0: Input Int [159] -- @p1: Input NVarChar [Shipper] -- @p2: Input NVarChar [Northwind Shipper] -- @p3: Input NVarChar [(123)-456-7890] -- @p4: Input NVarChar [Unknown] -- @p5: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Supplier] -- @p6: Input NVarChar (Size = 7; Prec = 0; Scale = 0) [Shipper] -- @p7: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Employee] -- @p8: Input NVarChar (Size = 8; Prec = 0; Scale = 0) [Customer]
版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载。
LINQ体验(18)——LINQ to SQL语句之视图和继承支持
标签:
原文地址:http://blog.csdn.net/u013948191/article/details/47393609