本文將通過ADO
枚舉(Enum)是一種常用的類型
這種方法需要使用POCO類
數據庫腳本
if exists (select
from sysobjects
where id = object_id(
and type =
drop table Account go create table Account
(
ID uniqueidentifier not null default NewSequentialID()
UserName nvarchar(
Password varchar(
Email nvarchar(
Role int not null
constraint PK_ACCOUNT primary key (ID)
)
insert into Account (UserName
insert into Account (UserName
insert into Account (UserName
這是一個用於存放帳號信息的數據表
我們按常規做法寫一個用於表示Role的枚舉類型
public enum AccountRoleEnum {
Admin =
User =
}
然後寫一個復雜類型用於在枚舉類型和數據庫的int類型之間做變換
public partial class RoleWrapper
{
private AccountRoleEnum m_orderStatus;
public int Value
{
get {
return (int)m_orderStatus;
}
set {
m_orderStatus = (AccountRoleEnum)value;
} }
public AccountRoleEnum EnumValue
{
get {
return m_orderStatus;
}
set {
m_orderStatus = value;
}
}
public static implicit operator RoleWrapper(AccountRoleEnum role)
{
return new RoleWrapper {
EnumValue = role
};
}
public static implicit operator AccountRoleEnum(RoleWrapper role)
{
if (role == null)
return AccountRoleEnum
return role
}
} 最後的
然後我們寫Account實體
public class Account
{
public Guid ID
{
get;
set;
}
public string UserName { get; set;
}
public string Password
{
get;
set;
}
public string Email
{
get;
set;
}
public RoleWrapper Role
{
get;
set;
} 和實體框架上下文
public class EntitiesContext : ObjectContext
{
public EntitiesContext()
: base(
{
_accounts = CreateObjectSet<Account>();
}
public ObjectSet<Account> Accounts
{
get
{
return _accounts;
}
}
private ObjectSet<Account> _accounts;
}
這樣
account
EntitiesContext db = new EntitiesContext(); db
From:http://tw.wingwit.com/Article/program/net/201311/12519.html