zoukankan      html  css  js  c++  java
  • Linq to SQL Xml Based

    SqlMetal 命令行工具可为 .NET Framework 的 LINQ to SQL 组件生成代码和映射。生成一个包含提取的 SQL 元数据的 .xml 文件:


    /code[: 文件]

    以源代码形式发送输出。不能与 /dbml 选项一起使用。
     
    /language:<language>

    指定源代码语言。

    有效的 <语言> 包括:vb、csharp。

    默认值:从代码文件的扩展名派生。

    /namespace:<name>

    为生成的代码指定命名空间。默认值:无命名空间。 

    sqlmetal /server:myserver /database:northwind /xml:mymeta.xml

    Email:
    Or, use this form.

    Guy Burstein The Bu 

    Linq to SQL Xml Based
    Mapping




    Linq to SQL Xml Based Mapping In the last post I talked about Linq
    to SQL Attribute Based Mapping
    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:

    class Order{

        public int Id { get; set; }

        public DateTime? OrderDate { get; set; }

        public string CustomerId { get; set; }

        private EntityRef<Customer> _customerRef;

        public Customer Customer

        {

            get { return this._customerRef.Entity; }

            set { this._customerRef.Entity = value; }

       }

    }

    and:

    public class Customer{

        public string CustomerId { get; set; }

    }

    The Xml Based Mapping schema should look like:

    <Database Name="Northwind"           xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007">


        <Type Name="XmlBasedMapping.Customer">

                  CanBeNull="false"               IsPrimaryKey="true" />


      </Table>  <Table Name="dbo.Orders" Member="Orders">


          <Column Name="OrderID"               Member="Id" IsPrimaryKey="true" IsDbGenerated="true" />


         <Column Name="CustomerID"


                 Member="CustomerId" DbType="NChar(5)" />      <Column Name="OrderDate"


                  Member="OrderDate" DbType="DateTime" />


          <Association Name="Orders_Customers"


                      Member="Customers"


                      ThisKey="CustomerId"


                      OtherKey="CustomerId"


                      IsForeignKey="true" />


        </Type>


      </Table>


    </Database>


    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

    DataContext ctx = new DataContext(connectionString, mapping); 

    var query = from order in ctx.GetTable<Order>()  where
    order.CustomerId == "ALFKI"

                select order;

    foreach (Order order in query)

    {    Console.WriteLine(order.Id + " " + order.OrderDate + " " + order.CustomerId);

    }

    Enjoy!

  • 相关阅读:
    【Python-虫师】自动化测试模型--参数化
    【Loadrunner】【浙江移动项目手写代码】代码备份
    虫师的性能测试思想html网页学习
    Loadrunner之https协议录制回放报错如何解决?(九)
    【Python虫师】多窗口定位
    【虫师讲Selenium+Python】第三讲:操作测试对象
    【虫师Python】第二讲:元素定位
    【小甲鱼】【Python】正则表达式(三)
    【小甲鱼】【Python】正则表达式(二)
    js提交数据时需判断是点击事件还是回车键
  • 原文地址:https://www.cnblogs.com/baiyu/p/2079915.html
Copyright © 2011-2022 走看看