Go to my github

LiteORM学习一:EntityObject 设计

LiteORM项目地址:http://www.codeproject.com/KB/database/lite.aspx

ListORM 怎么设计Entity呢?

listorm 是利用微软自带的Attribute 来实现数据库与实体类的映射过程。

image

lmgorm是利用xml来实现数据库与实体类的映射过程。这个会在LmgORM系列会有详细的介绍的。

image

using lite;
// maps to table dbo.person
[Table]
public class Person

// maps to table dbo.users
[Table(Name="users")]
public class User

// maps to table people.person
[Table(Schema="people")]
public class Person

// maps to view people.transactView
[Table(Name="transactView",Schema="people")]
public class Purchase

属性

// maps to [order_id]
[Column(Name="order_id")]
private int orderId;

// maps to [customer_id]
[Column(Name="customer_id")] // 这个用的比较多哦
public int CustomerId { get; set; }

// maps to [quantity]
[Column]
public int Quantity { get; set; }

主键

[Column, ID, PK] //加入了 ID 属性 代表是自增长 列
protected int studentNumber;

[Column(Name
= "UserID", Alias = "id"), PK] // 加入了 PK代码是主键
private int _UserID;

完整实体映射代码:

     [Table(Name = "User")]

    public class ListUser
    {
        [Column(Name 
= "UserID", Alias = "id"),ID, PK]
        
private int _UserID;

        [Column(Name 
= "UserName")]
        
private string _UserName;

        
public int UserID
        {
            
get { return _UserID; }
            
set { _UserID = value; }
        }
        
        
public string UserName
        {
            
get { return _UserName; }
            
set { _UserName = value; }
        }

        
#region
        
public override string ToString()
        {
            
string info = "UserID= " + UserID.ToString() + "  UserName=" + UserName;
            
return info;
        }
        
public int Add()
        {
            IDb db 
= DbFactory.Instance.GetDb();
            
int records = db.Insert(this);
            
return records;
        }
        
public void Update()
        {
            IDb db 
= DbFactory.Instance.GetDb();
            
int records = db.Update(this);
        }
        
public void Delete(string column)
        {
            IDb db 
= DbFactory.Instance.GetDb();
            IQuery q 
= db.Query();
            q.Constrain(column).Equal(
1);
            db.Delete(
this.GetType(), q);
        }
        
public ListUser Find(string id)
        {
            IDb db 
= DbFactory.Instance.GetDb();
            ListUser p2 
= (ListUser)db.Find(this.GetType(), id);
            
return p2;
        }
        
public System.Collections.IList Query()
        {
//查询
            IDb db = DbFactory.Instance.GetDb();
            IQuery q 
= db.Query();
            System.Collections.IList list 
= db.Select(this.GetType(), q);
            
return list;
        }
        
#endregion
    }

内部实现

TableAttribute  表映射

ColumnAttribute 列映射

PKAttribute 主键映射

SPResultAttribute 存储过程或视图的映射

不知道理解的对不对。以后在完善。

posted @ 2011-03-31 14:44  峡谷少爷  阅读(1845)  评论(0编辑  收藏  举报