• SubSonic3.0安装


    无意中知道这个ORM于是便baidu, google寻找安装方法, 都只有2.0的. 没有3.0,于是去官网看了下 被墙了 doc打不开,你懂的。还好包了个VPN

    一、引用SubSonic.Core.dll

    二、新建一个app.config或web.config加入如下代码

    <configuration>
      <connectionStrings>
        <add name="PSM"
           connectionString="server=.\SQLExpress;database=PSM;integrated security=true;"
           providerName="System.Data.SqlClient"/>
        <!-- For SQL Server -->
        <add name="NorthwindSQLite"
           connectionString="Data Source=F:\my.db"
           providerName="System.Data.SQLite"/>
        <!-- For SQLite -->
        <add name="NorthwindMySql"
           connectionString="server=localhost;database=northwind;user id=root; password="
           providerName="MySql.Data.MySqlClient"/>
        <!-- For MySQL -->
      </connectionStrings>
    </configuration>

    name :连接名称

    connectionString:连接数据库字符串

    prividerName: 驱动

    三、使用文本编辑器打开SubSonic 3.0.0.4\T4 Templates\ActiveRecord \Settings.ttinclude

    四、修改这三个属性

    Namespace:生成代码的命名空间

    ConnectionStringName:使用哪个连接(上面app.config配置的)

    DatabaseName:数据库

    五、添加ActiveRecord目录下的所有文件到项目中

    六、完成

    英文原文如下:

    Using ActiveRecord
     

    Contents

    [hide]

    Summary

    ActiveRecord is the simplest, easiest way to work with your data and your database. The pattern is that each instance you work with is, literally, a row of data in your database. This means that each object type can be thought of as a table in your database.

    Many developers don't like to work this "closely" with their database - however others view this kind of thing as absolutely ideal - removing the object-oriented "impedance mismatch" in favor of working with a thin database abstraction.

    SubSonic's approach to ActiveRecord is to give you what you need, then get out of your way. You'll find you have the ability to use Linq and simple, easy code-generation to get your data quick and simple-like.

    Setup

    Setting up SubSonic's ActiveRecord is pretty simple and involves four steps.

    Add a Reference to SubSonic

    Download and extract [SubSonic's files] onto your local drive. Place the SubSonic.Core.dll in a directory relative to your project or solution. Tip: Place the contents in a directory where your solution is located. Call it either Dependencies or Lib. Place all external dependencies (including SubSonic) in a folder like this.

    Then inside your project right-click the "Reference" folder, and select Add Reference. Browse to find the SubSonic.Core.dll file and include it.

    Add a Connection String

    In order to work with your database you'll need a connection string that is located in your project's app.config or web.config. If you have a seperate project for your data access code, you may need to create a new app.config in the root of the project.

    If you're working with a provider other than SQL Server (such as SQLite), you'll need to specify which one by changing the "providerName" attribute value:

      <configuration>
        ...
        <connectionStrings>
          <add name="Northwind" 
             connectionString="server=.\SQLExpress;database=SubSonic;integrated security=true;" 
             providerName="System.Data.SqlClient"/><!-- For SQL Server -->
          <add name="NorthwindSQLite" 
             connectionString="Data Source=C:\my.db" 
             providerName="System.Data.SQLite"/><!-- For SQLite -->
          <add name="NorthwindMySql" 
             connectionString="server=localhost;database=northwind;user id=root; password=" 
             providerName="MySql.Data.MySqlClient"/><!-- For MySQL -->
       </connectionStrings>
      </configuration>

    Set The Connection In The Templates

    Look in the Templates folder in the files extracted earlier. The T4 Templates that create the classes you're going to work with need to know what your connection string is. To set this, open up the file called "Settings.ttinclude". Below the @import statements, you'll see three string values you'll need to change:

        const string Namespace = "Northwind.Data";
        const string ConnectionStringName = "Northwind";
     
        //This is the name of your database and is used in naming
        //the repository. By default we set it to the connection string name
        const string DatabaseName = "Northwind";

    "Namespace" refers to the namespace that the generated subsonic code will be placed under.

      namespace Northwind.Data
      {
        public class YourEntity
        {
        ...
        }
      }

    You need to fill out "ConnectionStringName" with the connection string value that you placed in your app.config or web.config file earlier. This tells SubSonic which connection to use in the DB.

    Change the "DatabaseName" value to the name of the database specified in the connection string.

    The include directive in all *.tt files need to match your database. The ActiveRecord.tt on GitHub master, as of this writing, points to MySQL.ttinclude.

    // other values: MySQL.ttinclude, SQLite.ttinclude
    <#@ include file="SQLServer.ttinclude" #>

    Add the T4 Templates To Your Project

    After you've set the settings above, just drag into your project. Whenever Visual Studio 2008 sees a "tt" file, it will automatically execute it - so you don't have to do anything, it will just run.

    List of files to drag:

    • ActiveRecord.tt
    • Context.tt
    • Settings.ttinclude
    • SQLServer.ttinclude OR MySQL.ttinclude OR SQLite.ttinclude
    • StoredProcedures.tt - optional, add if you will invoke stored procedures
    • Structs.tt

    If you make a change to your database, just right-click the ActiveRecord.tt, Context.tt and Structs.tt files (CTRL+Click to select all *.tt files) and select "Run Custom Tool" and this will execute them again.

    If you have any errors at this point, you need to check your settings in the prior step.

    Querying

    The whole point of using ActiveRecord is to make querying easy. Here are some examples from our unit tests:

    //find a single product by ID
    var product = Product.SingleOrDefault(x => x.ProductID == 1);
     
    //get a list of products based on some criteria
    var products = Product.Find(x => x.ProductID <= 10);
     
    //get a list of server-side paged products
    var products = Product.GetPaged(0,10);
     
    //query using Linq
    var products = from p in Product.All()
              join od in OrderDetail.All() on p.ProductID equals od.ProductID
              select p;

    You'll notice that we've departed a bit from the way SubSonic 2.0 works (as well as other ActiveRecord libraries). Most other libraries use the constructor to pass in the key or name/value pair to find a single record. This can lead to some confusion if there is no record as your object will not be null (as it should be).

    For this reason we use a Factory to get single values - as you can see above.

    Testing

    Testing with ActiveRecord can be a major pain because it lives "so close" to your database. Rails uses the concept of a Test Database for this - basically saying "get over it" with respect to involving your DB in your unit tests. This can work fine for you - if you want to use a test database you can and ActiveRecord will work fine.

    To do this, simply set your connection string in your test project (in the App.config) to point to your test database, and you're good to go.

    If you want to have speedier tests with less of a chance of failure due to bad data or connection issues - you can work with SubSonic's built-in testing (for ActiveRecord). There's nothing you need to do other than use the connection string "Test" in your test project:

        <add name="Northwind" 
             connectionString="Test" 
             providerName="System.Data.SqlClient"/>

    This will tell SubSonic to use a Faked up repository (called TestRepository) which works with a fake set of data. You can set this up as you like using Setup():

    //add 100 products to the fake repository
    Product.Setup(100);
    //make sure they're there!
    Assert.Equal(100,Product.All().Count());

    You can also send in the list you want to work with if you want to mock up specific objects. See the constructor overloads for an example.

    You will be working against an in-memory List<Product> at this point (based on Setup()), so saving/deleting/editing will add/delete objects in the same way you would see with a database.

    Changing generated table class names in SubSonic 3

    Subsonic 2.0 had a feature that was modifiable via the webconfig called stripTableText that allowed you to modify the generated class names used by SubSonic.

    While not instantly obvious doing the same thing is even simpler and more powerful in subsonic 3 simply by editing the Settings.ttinclude file.

    The standard empty Settings.ttinclude file's CleanUp method:

    string CleanUp(string tableName){
      string result=tableName;
     
      //strip blanks
      result=result.Replace(" ","");
     
      //put your logic here...
     
      return result;
    }

    An simple example of the file edited to change tblTableName into TableName

    string CleanUp(string tableName){
      string result=tableName;
     
      //strip blanks
      result=result.Replace(" ","");
     
      //strip the phrase "tbl"
      result=result.Replace("tbl","");
     
      return result;
    }
  • 相关阅读:
    Codeforces Round #352 (Div. 1) B. Robin Hood 二分
    Codeforces Round #352 (Div. 1) A. Recycling Bottles 暴力
    Codeforces Round #352 (Div. 2) B. Different is Good 水题
    Codeforces Round #352 (Div. 2) A. Summer Camp 水题
    Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造
    Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力
    Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) B. Problems for Round 水题
    Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题
    Codeforces Beta Round #97 (Div. 1) C. Zero-One 数学
    Codeforces Beta Round #97 (Div. 1) B. Rectangle and Square 暴力
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3033206.html
Copyright © 2020-2023  润新知