自己动手写ORM框架(五):关系映射配置—Column属性
这次将完成最后一个自定义属性功能Column,在讲Column实现之前先看看Student表的结构如下:
create table student
(
studentid VARCHAR2(40),
studentno VARCHAR2(40),
name VARCHAR2(40),
address VARCHAR2(40),
telphone VARCHAR2(40)
)
然后来看看Column自定义属性的具体用法代码1-1:
using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using System.Orm.CustomAttributes;
namespace Entity
{
[Serializable]
[Table(Name="Student")]
public class StudentEntity
{
private string stuid;
private string stuno;
private string name;
private int sex;
private int age;
private string address;
private string telphone;
[Id(Name=”studentid”,Strategy = GenerationType.INDENTITY)]
public string Stuid
{
get { return stuid; }
set { stuid = value; }
}
[Column(Name="studentno")]
public string Stuno
{
get { return stuno; }
set { stuno = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
[Column(IsInsert = false,IsUpdate = false)]
public int Sex
{
get { return sex; }
set { sex = value; }
}
[Column(IsInsert = false, IsUpdate = false)]
public int Age
{
get { return age; }
set { age = value; }
}
public string Address
{
get { return address; }
set { address = value; }
}
public string Telphone
{
get { return telphone; }
set { telphone = value; }
}
}
}
在上面StudentEntity实体类的属性上配置了映射关系,对于Table、Id自定义属性的配置前面已经讲述过了,这里不再罗嗦,主要讲Column如何配置。
[Column(Name="studentid")] 这个配置加在属性public string Stuid { get …… } 上面,意思是该属性对应Student表中的studentid这一列。
而在public string Name{get…}上却没有配置,那么程序会认为属性名称Name和Student表中的列名一样,无需另外进行映射,顾Student表中也有Name这个列名。
[Column(IsInsert = false,IsUpdate = false)] 这里没有设置Column的Name的值,而是IsInsert和IsUpdate的值,意思是i该属性值不需要插入到数据库中,我们在做IsInsert和IsUpdate操作的时候可以对它进行忽略不处理。
下面看看Column自定义属性类的实现代码1-2:
using System;
using System.Collections.Generic;
using System.Text;
namespace System.Orm.CustomAttributes
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false, Inherited = false)]
public class ColumnAttribute : Attribute
{
private string _Name = string.Empty; //列名
private bool _IsUnique = false; //是否唯一
private bool _IsNull = true; //是否允许为空
private bool _IsInsert = true; //是否插入到表中
private bool _IsUpdate = true; //是否修改到表中
public ColumnAttribute() { }
public string Name
{
get { return _Name ; }
set { _Name = value; }
}
public bool IsUnique
{
get { return _IsUnique; }
set { _IsUnique = value; }
}
public bool IsNull
{
get { return _IsNull; }
set { _IsNull = value; }
}
public bool IsInsert
{ www.zzzyk.com
get { return _IsInsert; }
set { _IsInsert = value; }
}
public bool IsUpdate
{
get { return _IsUpdate; }
set { _IsUpdate = value; }
}
}
}
到此为止,自定义属性都已经完成,以后要讲的是根据实体类的配置实现增、删、改、查等功能。
补充:软件开发 , Java ,