我总结的三层结构之一:Model示例

我总结的三层结构之一:Model示例

简单字典表的C# Model类示例

数据表tFolk定义:

create table tFolk (

Id int identity(1,1) primary key,

Title nvarchar(50) not null unique

);

go

这是个简单的字典表,Id是主键;Title不允许空值,有唯一性约束。其对应Model类如下:

//============================================================

// Module:              C# model class of data table [tFolk]

// Auto generated by:     mySagasoft CodeAssistant for MSPetShop3Tiers of sagahu@163.com, at 2012-07-29 14:50:40

// Usage:                     

// Last modified by: sagahu@163.com

// Last modified at:  2012-07-29 14:50:40

//============================================================

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

namespace mySagasoft.CodeTemplates.Models   // 命名空间需要根据实际项目修改

{

    [Serializable]

    public partial class Folk

    {

        #region Constructors

        public Folk() { }

        public Folk(int? id, string title)

            : this()

        {

            this.Id = id;

            this.Title = title;

        }

        public Folk(DataRow dr)

            : this()

        {

            if (Convert.IsDBNull(dr["Id"])) this.Id = (int)dr["Id"];

            if (Convert.IsDBNull(dr["Title"])) this.Title = (string)dr["Title"];

        }

        #endregion

        #region Member variables

        private int? _Id;

        private string _Title;

        #endregion

        #region Properties

        public int? Id

        {

            get { return this._Id; }

            set { this._Id = value; }

        }

        public string Title

        {

            get { return this._Title; }

            set { this._Title = value; }

        }

        #endregion

        #region Methods

        public virtual string ToXmlString()

        {

            StringBuilder sb = new StringBuilder();

            sb.Append("<Entity>");

            sb.Append("<Id>"); sb.Append(this._Id.ToString()); sb.Append("</Id>");

            sb.Append("<Title>"); sb.Append(this._Title); sb.Append("</Title>");

            sb.Append("</Entity>");

            return sb.ToString();

        }

        #endregion

    }

}

简单业务数据表的C# Model类示例

数据表tStudent定义:

create table tStudent (

Id int identity(1,1) primary key,

Name nvarchar(50) not null,

       Birthday datetime,

       FolkId int,

       IdentityCode varchar(20) not null unique,

       Photo image

);

go

这是个比较简单的业务表,Id是主键;IdentityCode不允许空值,有唯一性约束;Photo是大对象(large object)类型的字段。

//============================================================

// Module:              C# model class of data table [tStudent]

// Auto generated by:     mySagasoft CodeAssistant for MSPetShop3Tiers of sagahu@163.com, at 2012-07-29 14:50:40

// Usage:                     

// Last modified by: sagahu@163.com

// Last modified at:  2012-07-29 14:50:40

//============================================================

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

namespace mySagasoft.CodeTemplates.Models   // 命名空间需要根据实际项目修改

{

    [Serializable]

    public partial class Student

    {

        #region Constructors

        public Student() { }

        public Student(int? id, string name, DateTime? birthday, int? folkId, string identityCode,

            byte[] photo)

            : this()

        {

            this.Id = id;

            this.Name = name;

            this.Birthday = birthday;

            this.FolkId = folkId;

            this.IdentityCode = identityCode;

            this.Photo = photo; // large object特殊处理

        }

        public Student(DataRow dr)

            : this()

        {

            if (Convert.IsDBNull(dr["Id"])) this.Id = (int)dr["Id"];

            if (Convert.IsDBNull(dr["Name"])) this.Name = (string)dr["Name"];

            if (Convert.IsDBNull(dr["Birthday"])) this.Birthday = (DateTime)dr["Birthday"];

            if (Convert.IsDBNull(dr["FolkId"])) this.FolkId = (int)dr["FolkId"];

            if (Convert.IsDBNull(dr["IdentityCode"])) this.IdentityCode = (string)dr["IdentityCode"];

            if (Convert.IsDBNull(dr["Photo"])) this.Photo = (byte[])dr["Photo"];

        }

        #endregion

        #region Member variables

        private int? _Id;

        private string _Name;

        private DateTime? _Birthday;

        private int? _FolkId;

        private string _IdentityCode;   // 身份证代码,唯一性值的字段

        private byte[] _Photo;  // large object 字段,需要特殊处理

        #endregion

        #region Properties

        public int? Id

        {

            get { return this._Id; }

            set { this._Id = value; }

        }

        public string Name

        {

            get { return this._Name; }

            set { this._Name = value; }

        }

        public DateTime? Birthday

        {

            get { return this._Birthday; }

            set { this._Birthday = value; }

        }

        public int? FolkId

        {

            get { return this._FolkId; }

            set { this._FolkId = value; }

        }

        public string IdentityCode

        {

            get { return this._IdentityCode; }

            set { this._IdentityCode = value; }

        }

        public byte[] Photo

        {

            get { return this._Photo; }

            set { this._Photo = value; }

        }

        #endregion

        #region Methods

        public virtual string ToXmlString()

        {

            StringBuilder sb = new StringBuilder();

            sb.Append("<Entity>");

            sb.Append("<Id>"); sb.Append(this._Id.ToString()); sb.Append("</Id>");

            sb.Append("<Name>"); sb.Append(this._Name); sb.Append("</Name>");

            sb.Append("<Birthday>"); sb.Append(this._Birthday.ToString()); sb.Append("</Birthday>");

            sb.Append("<FolkId>"); sb.Append(this._FolkId.ToString()); sb.Append("</FolkId>");

            sb.Append("<IdentityCode>"); sb.Append(this._IdentityCode); sb.Append("</IdentityCode>");           

            // sb.Append("<Photo>"); sb.Append(this._Photo); sb.Append("</Photo>"); // large object不加入这个方法

            sb.Append("</Entity>");

            return sb.ToString();

        }

        #endregion

    }

}

简单视图的C# Model类示例

视图vwStudentView定义:

create view vwStudentView

       as

       select s.Id,s.Name,s.Birthday,s.FolkId,f.Title,s.IdentityCode,s.Photo from tStudent as s

       left join tFolk as f on s.FolkId=f.Id;

go

在这个简单视图里,Id、IdentityCode是唯一性键值。

//============================================================

// Module:              C# model class of data view [vwStudentView]

// Auto generated by:     mySagasoft CodeAssistant for MSPetShop3Tiers of sagahu@163.com, at 2012-07-29 14:50:40

// Usage:                     

// Last modified by: sagahu@163.com

// Last modified at:  2012-07-29 14:50:40

//============================================================

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

namespace mySagasoft.CodeTemplates.Models   // 命名空间需要根据实际项目修改

{

    [Serializable]

    public partial class StudentView

    {

        #region Constructors

        public StudentView() { }

        public StudentView(int? id, string name, DateTime? birthday, int? folkId, string folkTitle,

            string _IdentityCode, byte[] photo)

            : this()

        {

            this.Id = id;

            this.Name = name;

            this.Birthday = birthday;

            this.FolkId = folkId;

            this.FolkTitle = folkTitle;

            this.IdentityCode = IdentityCode;

            this.Photo = Photo; // large object特殊处理

        }

        public StudentView(DataRow dr)

            : this()

        {

            if (Convert.IsDBNull(dr["Id"])) this.Id = (int)dr["Id"];

            if (Convert.IsDBNull(dr["Name"])) this.Name = (string)dr["Name"];

            if (Convert.IsDBNull(dr["Birthday"])) this.Birthday = (DateTime)dr["Birthday"];

            if (Convert.IsDBNull(dr["FolkId"])) this.FolkId = (int)dr["FolkId"];

            if (Convert.IsDBNull(dr["FolkTitle"])) this.FolkTitle = (string)dr["FolkTitle"];

            if (Convert.IsDBNull(dr["IdentityCode"])) this.IdentityCode = (string)dr["IdentityCode"];

            if (Convert.IsDBNull(dr["Photo"])) this.Photo = (byte[])dr["Photo"];

        }

        #endregion

        #region Member variables

        private int? _Id;

        private string _Name;

        private DateTime? _Birthday;

        private int? _FolkId;

        private string _FolkTitle;

        private string _IdentityCode;   // 身份证代码,唯一性值的字段

        private byte[] _Photo;  // large object 字段,需要特殊处理

        #endregion

        #region Properties

        public int? Id

        {

            get { return this._Id; }

            set { this._Id = value; }

        }

        public string Name

        {

            get { return this._Name; }

            set { this._Name = value; }

        }

        public DateTime? Birthday

        {

            get { return this._Birthday; }

            set { this._Birthday = value; }

        }

        public int? FolkId

        {

            get { return this._FolkId; }

            set { this._FolkId = value; }

        }

        public string FolkTitle

        {

            get { return this._FolkTitle; }

            set { this._FolkTitle = value; }

        }

        public string IdentityCode

        {

            get { return this._IdentityCode; }

            set { this._IdentityCode = value; }

        }

        public byte[] Photo

        {

            get { return this._Photo; }

            set { this._Photo = value; }

        }

        #endregion

        #region Methods

        public virtual string ToXmlString()

        {

            StringBuilder sb = new StringBuilder();

            sb.Append("<Entity>");

            sb.Append("<Id>"); sb.Append(this._Id.ToString()); sb.Append("</Id>");

            sb.Append("<Name>"); sb.Append(this._Name); sb.Append("</Name>");

            sb.Append("<Birthday>"); sb.Append(this._Birthday.ToString()); sb.Append("</Birthday>");

            sb.Append("<FolkId>"); sb.Append(this._FolkId.ToString()); sb.Append("</FolkId>");

            sb.Append("<FolkTitle>"); sb.Append(this._FolkTitle.ToString()); sb.Append("</FolkTitle>");

            sb.Append("<IdentityCode>"); sb.Append(this._IdentityCode); sb.Append("</IdentityCode>");

            // sb.Append("<Photo>"); sb.Append(this._Photo); sb.Append("</Photo>"); // large object不加入这个方法

            sb.Append("</Entity>");

            return sb.ToString();

        }

        #endregion

    }

}

原文地址:https://www.cnblogs.com/sagahu/p/2712694.html