Skip to content
This repository was archived by the owner on Mar 9, 2025. It is now read-only.
/ examples Public archive

Commit 1fed1c3

Browse files
committed
SqlServer2005 example.
1 parent 933a73b commit 1fed1c3

File tree

76 files changed

+9238
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+9238
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.40629.0
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqlServer2005", "SqlServer2005\SqlServer2005.csproj", "{9D5C79C8-3B06-434E-A87C-472D94016B79}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{9D5C79C8-3B06-434E-A87C-472D94016B79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9D5C79C8-3B06-434E-A87C-472D94016B79}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9D5C79C8-3B06-434E-A87C-472D94016B79}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9D5C79C8-3B06-434E-A87C-472D94016B79}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<connectionStrings>
4+
<add name="Example2005" connectionString="Server=DBHost\SQLSERVER2005;Database=Example;User Id=sa;Password=TestPassword;" providerName="SqlServer" />
5+
</connectionStrings>
6+
</configuration>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//---------------------------------------------------------------------------------------------------
2+
// <auto-generated>
3+
// This code was generated by T4Model template for T4 (https://github.com/linq2db/t4models).
4+
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
5+
// </auto-generated>
6+
//---------------------------------------------------------------------------------------------------
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq.Expressions;
10+
using System.Reflection;
11+
12+
using LinqToDB;
13+
using LinqToDB.DataProvider.SqlServer;
14+
using LinqToDB.Mapping;
15+
16+
namespace DataModels
17+
{
18+
/// <summary>
19+
/// Database : Example
20+
/// Data Source : DBHost\SQLSERVER2005
21+
/// Server Version : 09.00.5000
22+
/// </summary>
23+
public partial class ExampleDB : LinqToDB.Data.DataConnection
24+
{
25+
public ITable<TestTable> TestTables { get { return this.GetTable<TestTable>(); } }
26+
27+
public ExampleDB()
28+
{
29+
InitDataContext();
30+
}
31+
32+
public ExampleDB(string configuration)
33+
: base(configuration)
34+
{
35+
InitDataContext();
36+
}
37+
38+
partial void InitDataContext();
39+
40+
#region FreeTextTable
41+
42+
public class FreeTextKey<T>
43+
{
44+
public T Key;
45+
public int Rank;
46+
}
47+
48+
[FreeTextTableExpression]
49+
public ITable<FreeTextKey<TKey>> FreeTextTable<TTable,TKey>(string field, string text)
50+
{
51+
return this.GetTable<FreeTextKey<TKey>>(
52+
this,
53+
((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(TTable), typeof(TKey)),
54+
field,
55+
text);
56+
}
57+
58+
[FreeTextTableExpression]
59+
public ITable<FreeTextKey<TKey>> FreeTextTable<TTable,TKey>(Expression<Func<TTable,string>> fieldSelector, string text)
60+
{
61+
return this.GetTable<FreeTextKey<TKey>>(
62+
this,
63+
((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(TTable), typeof(TKey)),
64+
fieldSelector,
65+
text);
66+
}
67+
68+
#endregion
69+
}
70+
71+
[Table(Schema="dbo", Name="TestTable")]
72+
public partial class TestTable
73+
{
74+
[Identity ] public int ID { get; set; } // int
75+
[Column, NotNull] public DateTime CreatedOn { get; set; } // datetime
76+
}
77+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<#@ template language="C#" debug="True" hostSpecific="True" #>
2+
<#@ output extension=".generated.cs" #>
3+
<#@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude" #>
4+
<#@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude" #>
5+
<#
6+
/*
7+
1. Copy this file to a folder where you would like to generate your data model,
8+
rename it, and delete .txt extension. For example:
9+
10+
MyProject
11+
DataModels
12+
MyDatabase.tt
13+
14+
2. Modify the connection settings below to connect to your database.
15+
16+
3. Add connection string to the web/app.config file:
17+
18+
<connectionStrings>
19+
<add name="MyDatabase" providerName="System.Data.SqlClient"
20+
connectionString="Data Source=.;Database=MyDatabase;User Id=User;Password=TestPassword;" />
21+
</connectionStrings>
22+
23+
4. To access your database use the following code:
24+
25+
using (var db = new MyDatabaseDB())
26+
{
27+
var q =
28+
from c in db.Customers
29+
select c;
30+
31+
foreach (var c in q)
32+
Console.WriteLine(c.ContactName);
33+
}
34+
35+
5. See more at https://github.com/linq2db/t4models/blob/master/Templates/ReadMe.LinqToDB.md.
36+
37+
If you need to use the Microsoft.SqlServer.Types namespace, install the Microsoft.SqlServer.Types nuget,
38+
and replace the following include at the top of this file:
39+
40+
"$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude"
41+
42+
with
43+
44+
"$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.SqlTypes.Tools.ttinclude"
45+
*/
46+
47+
NamespaceName = "DataModels";
48+
49+
LoadSqlServerMetadata(@"DBHost\SQLSERVER2005", "Example", "sa", "TestPassword");
50+
// LoadSqlServerMetadata(string connectionString);
51+
52+
GenerateModel();
53+
#>
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<#@ template language="C#" debug="True" hostSpecific="True" #>
2+
<#@ output extension=".generated.cs" #>
3+
<#@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude" #>
4+
<#@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude" #>
5+
<#
6+
/*
7+
1. Copy this file to a folder where you would like to generate your data model,
8+
rename it, and delete .txt extension. For example:
9+
10+
MyProject
11+
DataModels
12+
MyDatabase.tt
13+
14+
2. Modify the connection settings below to connect to your database.
15+
16+
3. Add connection string to the web/app.config file:
17+
18+
<connectionStrings>
19+
<add name="MyDatabase" providerName="System.Data.SqlClient"
20+
connectionString="Data Source=.;Database=MyDatabase;User Id=User;Password=TestPassword;" />
21+
</connectionStrings>
22+
23+
4. To access your database use the following code:
24+
25+
using (var db = new MyDatabaseDB())
26+
{
27+
var q =
28+
from c in db.Customers
29+
select c;
30+
31+
foreach (var c in q)
32+
Console.WriteLine(c.ContactName);
33+
}
34+
35+
5. See more at https://github.com/linq2db/t4models/blob/master/Templates/ReadMe.LinqToDB.md.
36+
37+
If you need to use the Microsoft.SqlServer.Types namespace, install the Microsoft.SqlServer.Types nuget,
38+
and replace the following include at the top of this file:
39+
40+
"$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude"
41+
42+
with
43+
44+
"$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.SqlTypes.Tools.ttinclude"
45+
*/
46+
47+
NamespaceName = "DataModels";
48+
49+
LoadSqlServerMetadata("MyServer", "MyDatabase", "User", "Password");
50+
// LoadSqlServerMetadata(".", "MyDatabase"); // Integrated Security
51+
// LoadSqlServerMetadata(string connectionString);
52+
53+
GenerateModel();
54+
#>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<#
2+
{
3+
var beforeGenerateModel = BeforeGenerateModel;
4+
BeforeGenerateModel = () =>
5+
{
6+
beforeGenerateModel();
7+
DataAnnotationsImpl();
8+
};
9+
}
10+
#>
11+
<#+
12+
void DataAnnotationsImpl()
13+
{
14+
foreach (Class cl in GetTreeNodes(Model).OfType<Class>())
15+
{
16+
foreach (var p in GetTreeNodes(cl).OfType<Property>())
17+
{
18+
if (p.DisplayName != null)
19+
{
20+
p.Attributes.Add(new Attribute("Display", "Name=\"" + p.DisplayName + "\"") { IsSeparated = true });
21+
}
22+
23+
if (p.IsRequired)
24+
{
25+
var attr = new Attribute("Required") { IsSeparated = true };
26+
27+
if (p.IsRequiredMessage != null)
28+
attr.Parameters.Add(string.Format("ErrorMessage=\"" + p.IsRequiredMessage + "\"", p.DisplayName ?? p.Name));
29+
30+
p.Attributes.Add(attr);
31+
}
32+
33+
if (p.StringLength > 0)
34+
{
35+
var attr = new Attribute("StringLength", p.StringLength.ToString()) { IsSeparated = true };
36+
37+
if (p.StringLengthMessage != null)
38+
attr.Parameters.Add(string.Format("ErrorMessage=\"" + p.StringLengthMessage + "\"", p.DisplayName ?? p.Name));
39+
40+
p.Attributes.Add(attr);
41+
42+
// p.Attributes.Add(
43+
// new Attribute("StringLength",
44+
// p.StringLength.ToString(),
45+
// string.Format(
46+
// "ErrorMessage=\"The {0} must be a string with a maximum lenfth of {1}.\"",
47+
// p.DisplayName ?? "field",
48+
// p.StringLength))
49+
// {
50+
// IsSeparated = true
51+
// });
52+
}
53+
}
54+
}
55+
}
56+
57+
partial class Property
58+
{
59+
public string DisplayName;
60+
public bool IsRequired;
61+
public string IsRequiredMessage;
62+
public int StringLength;
63+
public string StringLengthMessage;
64+
}
65+
#>

0 commit comments

Comments
 (0)