Linq to Entity 的T4 模板生成代码

   Linq to Entity目前不是太成熟的framework,但有时还会用到。这个放一个使用T4模板生成对应的Code,以及生成存储过程。注意模型生成模板,应与.edmx文件名相同。如你有一个Model1.edmx,那么这个模板命名是Model1.tt。看以下生成的Code片断:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from the EFT4 template v0.1.
//     Generation date: 10/16/2009 3:08:33 PM
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
 
using System;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.EntityClient;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;
 
[assembly: EdmSchema]
 
#region EDM Relationship Metadata
[assembly: EdmRelationship("AdventureWorksModel", "FK_VendorAddress_Address_AddressID", "Address", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(AdventureWorksModel.Address), "VendorAddress", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(AdventureWorksModel.VendorAddress))]
[assembly: EdmRelationship("AdventureWorksModel", "FK_VendorAddress_Vendor_VendorID", "Vendor", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(AdventureWorksModel.Vendor), "VendorAddress", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(AdventureWorksModel.VendorAddress))]
#endregion

 

以及另一个生成SQL片断:

--------------------------------------------------------------------------------
-- <auto-generated>
--     This code was generated by a tool.
--
--     Changes to this file may cause incorrect behavior and will be lost if
--     the code is regenerated.
-- </auto-generated>
--------------------------------------------------------------------------------
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- Creating stored procedures for d:\temp\ConsoleApplication1\Model1.edmx.
USE "AdventureWorks"
GO

 

EF支持SP调用的。自动生成方便得多,这些T4模板是值得学习的好资源。

 

模板下载

同样也有:LINQ to SQL templates for T4

希望这篇Post对您有帮助。

 

Author PetterLiu    http://wintersun.cnblogs.com

 

你可能感兴趣的:(entity)