生成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]
6.UseAsDefault形式
当插入一条记录时,使用默认的映射关 系了,但是在查询时,使用继承的关系了。具体看看生成的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]
7.插入新 的记录
这个例子说明如何插入发货人的联系方式的一条记录。
//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].[Compa
|