博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linq to SQL Xml Based Mapping
阅读量:6984 次
发布时间:2019-06-27

本文共 2380 字,大约阅读时间需要 7 分钟。

In the last post I talked about that lets you map CLR Objects to database objects using attributes. Although this approach is very simple and easy, it is compiled with the code and cannot be changed without recompiling. Another approach is Linq to SQL Xml Based Mapping that maps entities to database objects according to an xml file that is loaded in runtime.

So, given the two entities from the previous post, that have no mapping attributes at all:

classOrder {
publicint Id { get; set; } publicDateTime? OrderDate { get; set; } publicstring CustomerId { get; set; } privateEntityRef
_customerRef; publicCustomer Customer {
get { returnthis._customerRef.Entity; } set { this._customerRef.Entity = value; } } }

 

and:

publicclassCustomer {
publicstring CustomerId { get; set; } } The Xml Based Mapping schema should look like:

 

The root element is the Database element. The child elements are the database objects the are included in the mapping - Customers and Orders tables from the Northwind database. Each table can have child types that are mapped to entities in the application. This hierarchy sits also with the concept of inheritance in Linq to SQL since it only supports the Table per Class Hierarchy strategy. In the above example each table is mapped to a single entity. Notice that each table column is mapped to a member in the class.

To work with this mapping source, we should load it from a file / stream / url or any other resource, and supply it as a parameter for the DataContext instance we want to work with.

string connectionString = "...";

 

// Load the Mapping from a file XmlMappingSource mapping = XmlMappingSource.FromUrl("NorthwindMap.xml"); // Create a DataContext to the database, and supply // the url for the mapping file DataContext ctx = newDataContext(connectionString, mapping); var query = from order in ctx.GetTable
() where order.CustomerId == "ALFKI" select order; foreach (Order order in query) {
Console.WriteLine(order.Id + "" + order.OrderDate + "" + order.CustomerId); }

 

Enjoy!

转载于:https://www.cnblogs.com/xuhongbo/archive/2011/11/17/2253232.html

你可能感兴趣的文章
在面试中如何展示虚拟机和内存调优技能
查看>>
hdu 1534 Schedule Problem (差分约束)
查看>>
HDU1159 Common Subsequence【最长公共子序列】
查看>>
C++ 字符数组函数与string函数
查看>>
无限极分类优化方式
查看>>
从运维的角度理解Iaas、Paas、Saas云计算
查看>>
使用动画播放文件夹中的图片
查看>>
logging模块
查看>>
C# 将string 转换为二维码图片,然后转为base64字符串编码 。
查看>>
软件工程概论03
查看>>
js截取最后一个斜杠之后的内容
查看>>
Java程序安装失败
查看>>
解决网络请求的依赖关系
查看>>
设计模式(4)建造者模式/生成器模式(Builder)
查看>>
hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) ...
查看>>
开闭原则
查看>>
Silverlight 4常用StringFormat格式总结
查看>>
think in uml-关系
查看>>
androidSDK配置环境变量
查看>>
BZOJ 1003 物流运输 最短路+dp
查看>>