How to: Map a Database View to a Persistent Class(如何:映射数据库视图到持久化类)
代码
首先创建一个结构体,结构体中的属性为要绑定的视图列
public struct MyViewKey
{
[Persistent("Name")]
public string Name;
[Persistent("PhoneCode")]
public string PhoneCode;
[Persistent("Street")]
public string Street;
[Persistent("City")]
public string City;
[Persistent("FullAddress")]
public string FullAddress;
}
然后将这个结构体当成是Persistent类的KEY
[Persistent("TestView")] //TestView为视图名
public class MyView : XPLiteObject {
[Key, Persistent]
public MyViewKey Key;
public string Name{ get { return Key.Name; } }
public string PhoneCode { get { return Key.PhoneCode; } }
public string Street { get { return Key.Street; } }
public string City { get { return Key.City; } }
public string FullAddress { get { return Key.FullAddress; } }
}
这样就完成了
用到的视图Sql:
SELECT dbo.Country.Name, dbo.Country.PhoneCode, dbo.Address.Street, dbo.Address.City,
dbo.Address.FullAddress
FROM dbo.Address INNER JOIN
dbo.Country ON dbo.Address.Country = dbo.Country.Oid
Address:
CREATE TABLE [dbo].[Address] (
[Street] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[City] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[StateProvince] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[ZipPostal] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[FullAddress] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[OptimisticLockField] [int] NULL ,
[GCRecord] [int] NULL ,
[OID] [int] IDENTITY (1, 1) NOT NULL ,
[Country] [int] NULL
) ON [PRIMARY]
Country:
CREATE TABLE [dbo].[Country] (
[Oid] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[PhoneCode] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[OptimisticLockField] [int] NULL ,
[GCRecord] [int] NULL
) ON [PRIMARY]
GO
引自:http://www.cnblogs.com/dingshouqing/articles/1332715.html
public struct MyViewKey
{
[Persistent("Name")]
public string Name;
[Persistent("PhoneCode")]
public string PhoneCode;
[Persistent("Street")]
public string Street;
[Persistent("City")]
public string City;
[Persistent("FullAddress")]
public string FullAddress;
}
然后将这个结构体当成是Persistent类的KEY
[Persistent("TestView")] //TestView为视图名
public class MyView : XPLiteObject {
[Key, Persistent]
public MyViewKey Key;
public string Name{ get { return Key.Name; } }
public string PhoneCode { get { return Key.PhoneCode; } }
public string Street { get { return Key.Street; } }
public string City { get { return Key.City; } }
public string FullAddress { get { return Key.FullAddress; } }
}
这样就完成了
用到的视图Sql:
SELECT dbo.Country.Name, dbo.Country.PhoneCode, dbo.Address.Street, dbo.Address.City,
dbo.Address.FullAddress
FROM dbo.Address INNER JOIN
dbo.Country ON dbo.Address.Country = dbo.Country.Oid
Address:
CREATE TABLE [dbo].[Address] (
[Street] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[City] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[StateProvince] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[ZipPostal] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[FullAddress] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[OptimisticLockField] [int] NULL ,
[GCRecord] [int] NULL ,
[OID] [int] IDENTITY (1, 1) NOT NULL ,
[Country] [int] NULL
) ON [PRIMARY]
Country:
CREATE TABLE [dbo].[Country] (
[Oid] [int] IDENTITY (1, 1) NOT NULL ,
[Name] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[PhoneCode] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[OptimisticLockField] [int] NULL ,
[GCRecord] [int] NULL
) ON [PRIMARY]
GO
引自:http://www.cnblogs.com/dingshouqing/articles/1332715.html
欢迎转载,转载请注明出处:http://www.cnblogs.com/Tonyyang/