August 17, 2014

LINQ to SqlServer

  • Create new project.
  • Add the linq2db.SqlServer nuget to the project.
  • Copy the LinqToDB.Templates\CopyMe.SqlServer.tt.txt to a folder where you would like to generate your data model, rename it, and delete .txt extension.
  • Modify the connection settings in the T4 template to connect to your database.
    <#@ template language="C#" debug="True" hostSpecific="True"                           #>
    <#@ output extension=".generated.cs"                                                  #>
    <#@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.SqlServer.Tools.ttinclude" #>
    <#@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude"     #>
    <#
        NamespaceName = "DataModel";
    
        LoadSqlServerMetadata(@"DBHost\SqlServer2012", "Northwind", "sa", "TestPassword");
        GenerateModel();
    #>
    
  • Add connection string to the web/app.config file:
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <connectionStrings>
            <add name="Northwind" providerName="System.Data.SqlClient"
                connectionString="Data Source=DBHost\SqlServer2012;Database=Northwind;User Id=sa;Password=TestPassword;" />
        </connectionStrings>
    </configuration>
    
  • To access your database use the following code:
    using System;
    using System.Diagnostics;
    using System.Linq;
    
    using DataModel;
    
    using LinqToDB.Data;
    
    namespace LinqToDBDemo
    {
        // Watch video on http://youtu.be/Qc-5UpMYQO0
        //
        class Program
        {
            static void Main(string[] args)
            {
    #if DEBUG
                DataConnection.TurnTraceSwitchOn();
                DataConnection.WriteTraceLine = (s, s1) => Debug.WriteLine(s, s1);
    #endif
    
                using (var db = new NorthwindDB())
                {
                    var q =
                        from c in db.Customers
                        select new
                        {
                            c.CompanyName,
                            OrderCount = c.Orders.Count()
                        };
    
                    foreach (var c in q)
                        Console.WriteLine(c);
                }
            }
        }
    }
    

3 comments:

  1. 고객센터를 통한 재확인 문의 등이 먼저 이루어지지 않을 경우 퍼스트카지노는 이러한 사항을 미리 알지 못 할 경우가 있습니다. 강남 연세사랑병원 관계자는 “자기공명영상를 이용해 무릎 관절을 정확하게 측정한 후 3D 시물레이션을 이용해 연구한 결과 특히 한국인은 남녀별로 대퇴골 및 경골 모양, 회전 정렬의 축도 차이를 보였다”고 온라인바카라 【 woorisayi.com 】 가 설명했다. 퍼스트카지노 – 바카라 온라인대표 더킹오브딜러의 신규브랜드 (구)더킹오브딜러 온라인카지노 TOP3 더킹카지노,퍼스트카지노,예스카지노 바카라 이벤트 진행중이니 놓치지마세요. 홀덤사이트 홀덤777은 수년간 검증을 하며, 온라인홀덤 안전사이트를 소개해 드리기 위해 노력하고 있습니다.

    ReplyDelete
  2. One of the best combo choices is FOX Bet’s on-line on line casino, which is contained contained in the FOX Bet PA sportsbook app and offers each sports betting and on-line on line casino choices in Pennsylvania. Contrary to well-liked perception, the 온라인카지노 best on-line casinos provide much more than simply slots. There is an effective buyer support surroundings with telephone and e-mail choices available. The web site additionally features a reside chat service, however solely registered gamers can access it.

    ReplyDelete
  3. Whether you’re right here to hunt for treasure, take a swim underneath the deep blue sea, or explore the Egyptian pyramids search out|to search out} the Pharaoh’s riches, your options are nearly limitless. Bitstarz’s mobile-friendly web site mimics the functionality we’d expect to see from a downloadable utility. One thing’s sure – you’ll by no means discover a recreation like Bitstarz Billion in Vegas. Players trying to find a real-money on line casino need look no additional than Ducky Luck. You’ll have to satisfy 25x wagering 원 엑스 벳 requirements across the board.

    ReplyDelete