DataVeryLite 是基于.net 4.0的数据库持久化ORM框架.
1.连接字符串配置
a,App.config/Web.config配置方式
<add name="myConnetion" connectionString="Data Source=host;Initial Catalog=db;Integrated Security=True" providerName="sqlserver"/>
需要说明的是 name connectionString和providerName三个属性缺一不可
DataBase | Connection String sample | providerName |
---|---|---|
Sqlite | Data Source=path;Version=3; | sqlite |
Access | Provider=Microsoft.Jet.OLEDB.4.0;Data Source=path | access |
MySql | server = host; user id = name; password = pwd; database = db; | mysql |
PostgreSql | Server= host;User Id=name;Password=pwd;Database=db; | postgresql |
SqlServer | Data Source=host;Initial Catalog=db;Integrated Security=True | sqlserver |
Db2 | Database=db;User ID=name;Server=host;password=pwd | db2 |
Oracle | Data Source=XE;User Id=name;Password=pwd; | oracle |
public class MyConnectionStringProvider : ConnectionStringSettingsProvider { public override List<ConnectionStringSettings> ToConnectionString() { var sqlServverConnStr = new ConnectionStringSettings { Name = "sqlserver1", ConnectionString = "Data Source=host;Initial Catalog=db;Integrated Security=True", ProviderName = "sqlserver" }; var mysqlConnStr = new ConnectionStringSettings { Name = "mysql1", ConnectionString = "server = host; user id = name; password = pwd; database = db;", ProviderName = "sqlserver" }; return new List<ConnectionStringSettings>() { sqlServverConnStr,mysqlConnStr }; } }
2,数据库映射
[DataBase(Key = "sqlserver1")] public class Dandelion : DataVeryLite.Core.EntityPool { }
DataBaseAttribute中的Key属性对应的连接字符串配置中的key值,这样Dandelion会自动加载数据库上下文信息。
[Table(Name = "Person" , EntityPool = typeof(Dandelion))] public partial class Person : DataVeryLite.Core.Entity { [Column(Name = "Id", IsPrimaryKey = true)] public int Id { get; set; } [Column(Name = "Name")] public string Name{ get; set; } [Column(Name = "Sex")] public string Sex { get; set; } [Column(Name = "Phone")] public string Phone { get; set; } [Column(Name = "Email")] public string Email { get; set; } }
TableAttribute中的Name为表的名字。可以不设置,这时Person的类名就对应表的名字,如果我们需要类名和表名不一致,这时可以设置Name。
Configure.SetKey = (sender) => "sqlserver1";
或
Configure.SetKey = (sender) => { if (sender is Dandelion) { return "sqlserver1"; } else { return "mysql1"; } };
1 |
PM> Install-Package DataVeryLite |
using System; public class HelloWorld { public static void Main(params string[] args) { var p=Models.Xe.Person(); p.Load(By.Id(1)); p.Del(); Console.WriteLine(p.Name+","+p.Age); } }
DataVeryLite入门教程(一) 配置篇,布布扣,bubuko.com
原文:http://www.cnblogs.com/shuqizhao/p/3695107.html