PLINQO, which stands for Professional LINQ to Objects, is a collection of CodeSmith templates that are meant to replace and extend the LINQ to SQL designers that are included with Visual Studio 2008.
The templates have the following features.
Use the following steps to get started using the Linq to Sql templates.
The Dbml.cst template is used to create a LINQ to SQL dbml file. The file conforms to the Microsoft DbmlSchema.xsd schema. This is the same document that the LINQ to SQL designer uses. The generated dbml file from this template can also be edited from the LINQ to SQL designer.
The template will create a new file if it doesn't exist. If the file does exist, the template will read it in and update it. This allows you to make changes to the file and not have it overwrite if the template is re-ran. However, only some of the attributes are safe from overwriting. Here is a list of safe attributes. They will be listed as an xpath.
Safe Attributes to change in the Dbml file ...
Warning: Be aware that the template will drop tables, columns and associations that it did not find in the database.
Properties on the Dbml.cst template:
The entities template generates the entity classes needed by LINQ. The classes are generated from a dbml file. You can modify the names for classes and properties by editing the dbml file. See Dbml.cst for a list of safe attributes to change in the dbml file.
The template will generate 2 files for every Type in the dbml file. One file will be the generated partial class that can not be changed as it is overwritten when the template is re-ran. It will have the following file name... <entity>.Generated.cs
The second file is a partial class that can be modified as it will not be re-generated. You can implement the partial methods in this file. Some partial method stubs are created by default. This file will be named... <entity>.cs
If you set the project file property on the template, the generated files will be added to the project. The file that can not be modified will be hidden under the file that can be changed.
Properties on the Entities.cst template:
The manager template is for helping you get started with business logic for the LINQ entities. The managers will have common queries that are created from keys and indexes on the table. The manager will also have rules for the entity properties to make sure required fields are not null and that the length of a string does not exceed the max length the column allows.
The template works by creating a second DataContext class that has a Manager property. The manager will then have a property for each entity that has a manager. Here is a sample of the syntax for using the managers:
SampleManagerContext db = new SampleManagerContext();
// use the primary key
Task task = db.Manager.Task.GetByTaskID(taskId);
// use a foreign key
var myTasks = db.Manager.Task.GetByAssignedID(userId);
// the methods return IQueryable so you can add expressions
var openTasks = db.Manager.Task.GetByStatusID(statusId).OrderBy(t => t.CreateDate);
The manager also provides a business rules engine to your entities. In addition to the default validation rules that are generated, you can add custom rules by implementing the AddRules partial method in the custom entity class.
static partial void AddRules()
{
// Rule allows the Name property to be a max of 150 characters.
RuleManager.AddShared<Task>(new LengthRule("Name", 150));
// Rule that validates the value of the property using regex.
RuleManager.AddShared<Task>(new RegexRule("Name", ".*"));
// Rule allows only users in certain security roles to update.
RuleManager.AddShared<Task>(new UpdateRule(
new string[] { "Administrator", "Updaters" }));
}
Properties on the Managers.cst template:
Download the latest release from Google Code.
Pingback from PLINQO - Supercharge LINQ to SQL - Paul Welter
Linq to sql的Codesmith模板,非常好用:)
PLINQO, which stands for Professional LINQ to Objects, is a collection of CodeSmith templates that are
Hi Paul,
Great templates!
The only problem is that I can't get Northwind stored procedures to be compiled I get wrong properties generation - empty names for one property...
Alex,
You can change the name of the function that is a duplicate by editing the Function/@Method attribute in the dbml file. That should get you by this problem for now. I will add a check for function names matching table names.
~ Paul
Thanks Paul, but the problem is not duplicate in the function name, but wrong name of the properties for function parameters...
Do you compile generated code for Northwind database without problems?
I'm experiencing all kinds of problems when I use these templates:
1. "The using directive for System.Linq appeard previously in this namespace"
Getting this on at least all of my manager classes
2. "The type or namespace name 'Codesmith' could not be found ..."
Getting this all over the place as well.
Anyways, its been about 4 hours of trying to figure out how to resolve these problems ... and nada.
HELP!!!!
thanks - wg
One more exception I'm getting (using Northwind as my sample DB):
"The type 'LTS.BL.NorthwindDataContext' already contains a definition for 'SalesByCategory'"
Yes, I was able to get the generated code to compile from northwind after i renamed the SalesByCategory function get solve the duplicate name issue. Maybe you could send me a sample of what is failing for you.
wgpubs,
I forgot to mention that you need to add a ref to the CodeSmith.Data project that is included in the zip file.
Your issue with SalesByCategory is that there is a view by that name that ends up as a property on the datacontext. There is also a stored procedure by that name, so you get a method by the same name. You'll need to edit the dbml file to rename the function as stated in another comment.
Not sure about the "System.Linq appeard previously ..." error. After making the above changes and you still get the error, feel free to send me a sample so i can debug.
thanks for the comments,
PLINQO, which stands for Professional LINQ to Objects, is a collection of Code Smith templates...
Schema.cs
===========
public static class DbmlFile
private static XmlSerializer _serializer = new XmlSerializer(typeof(Database), DbmlConstant.DbmlNamespace);
public static void Save(Database db, string fileName)
XmlWriterSettings ws = new XmlWriterSettings();
ws.Encoding = Encoding.UTF8;
ws.Indent = true;
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("", DbmlConstant.DbmlNamespace);
using (StreamWriter sw = new StreamWriter(fileName))
XmlWriter w = XmlWriter.Create(sw, ws);
_serializer.Serialize(w, db, xsn);
w.Flush();
w.Close();
Debug.WriteLine("Saving Dbml File:" + fileName);
/* StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
File.WriteAllText(fileName, sb.ToString(), Encoding.UTF8);
*/
=========================
应做如上改动以输出:正确编码的DBML文件
PLINQO (pronounced “Plinko”) or Professional LINQ to SQL is a collection of CodeSmith templates
PLINQO, which stands for Professional LINQ to Objects, is a collection of CodeSmith templates that are meant to replace and extend the LINQ to SQL designers that are included with Visual Studio 2008. Features Generate or update a LINQ to SQL dbml file