zoukankan      html  css  js  c++  java
  • Linq to SQL T4 代码生成器 (四)访问设计器中的 Association 对象

    要访问设计器中 Association 对象,需要通过 IType.Association 属性进行访问的。而生成 Association 的代码,也比较啰嗦,因此在这里只是把一些要点写出来,并不是完整的代码。完整的代码请参我在第一篇文章所提供的示例。

    在从 Northwind 数据库中,拖 Categores 和 Products 两个表到设计器中。如下载所示:

    先来看一下一段简单的模版代码,当然,这段模版代码代仅用来演示。

    <#@ template inherits="ModelingTextTransformation" language="C#" debug="true" hostspecific="True"#>
    <#@ QuickCode processor="DbmlProcessor" requires="ModelFile='Northwind.dbml'"#>
    <#@ output extension=".cs" #>
    <#@ import namespace = "System.Text.RegularExpressions" #>

    using System.Data.Linq;
    using System.Data.Linq.Mapping;

    namespace <#= DataContext.ContextNamespace #>
    {
    <# foreach(ITable table in DataContext.Tables){ #>
    [Table(Name
    ="<#= table.Name #>")]
    public partial class <#= table.Type.Name #>
    {
    <# foreach(IAssociation association in table.Type.Associations){ #>
    [Association(ThisKey
    ="<#= String.Join(",",association.ThisKey) #>", OtherKey="<#= String.Join(",",association.OtherKey) #>")]
    <# if(association.Cardinality == Cardinality.One) {#>
    public <#= association.Type #> <#= association.Member #>
    {
    get;
    set;
    }
    <# }else{ #>
    public EntitySet<<#= association.Type #>> <#= association.Member #>
    {
    get;
    set;
    }
    <# } #>
    <# } #>
    }
    <# } #>
    }

    生成的代码如下:

    代码
    using System.Data.Linq;
    using System.Data.Linq.Mapping;

    namespace DecodeDemo
    {

    [Table(Name
    ="dbo.Categories")]
    public partial class Category
    {
    [Association(ThisKey
    ="", OtherKey="CategoryID")]
    public EntitySet<Product> Products
    {
    get;
    set;
    }

    }

    [Table(Name
    ="dbo.Products")]
    public partial class Product
    {
    [Association(ThisKey
    ="CategoryID", OtherKey="")]
    public Category Category
    {
    get;
    set;
    }

    }

    }

    有几个属性解释一下:

    association.Type 表示的是 Association 目标对象的类型(也就是 Association 的另一端)

    就拿来本文中的例子来,当然 Category 实体类来说,它的 association.Type 为 Product 类型,而 Product 实体类的 association.Type 则为 Category。

    association.Cardinality 表示另一端是的对象是一个或者多个,也就是一对一、还是一对多的关系。

    association.Cardinality == Cardinality.One 表示是一对一

    association.Cardinality == Cardinality.Many 表示一对多。

     

    association.ThisKey 、association.OtherKey 表示的是主键和连接的外键,而且它们都是字符串数组,所以生成代码的时候要用 String.Join 方法。

    association.Member 表示 Association 所在类中的成员名称。

    完整的成员请访问:http://www.alinq.org/document/decode.htm#IAssociation 

    关于 Association 对象的访问就到此为止了,如有任何疑问,可以给我留言。

    预告:

    接下来可能会写的内容是:

    1、单模版生多文件。(现在生成的都是一个文件)

    2、给大家介绍一个新的 Linq to SQL 设计器,当然,我所提供的设计器是在原在的基础上进行改进的。

    3、存储过程的代码生成。(可能不写,因为很少有人需要自定义存储过程的生成代码)

    4、生成 NHiberate 代码。NHibernate 最缺乏的就是一个良好的设计器,相信有不少使用 NHibernate 的朋友会感兴趣的。

  • 相关阅读:
    IDEA快捷键收集
    Jmeter录制HTTPS
    Jmeter 线程组、运行次数参数化
    fiddler 抓取iphone发出的http和https包
    Appium 点击屏幕
    安卓程序如何保证低内存下依然存在
    listview
    数据库操作
    数据存储
    测试
  • 原文地址:https://www.cnblogs.com/ansiboy/p/1785006.html
Copyright © 2011-2022 走看看