NetTiers学习总结2--数据库

第二次 数据库 建库原则:

表名称采用单数和Pascal命名法。当然。例如: FirstName, LastName, MiddleInitial; 为表提供表描述,列和键等扩展属性;

使用默认值,默认情况下,您的实体属性使用您的数据库默认值。紧记,默认值只有简单的常量和少数职能是支持,如 getdate() ;

.nettiers不能建议关系,可以使用建立外键关联。

如:建立以下表:Person和Contact

1-- Create the Base Relationship, has a unique primary key amongst both tables. 2CREATE TABLE [dbo].[Person]( 3 [PersonId] [int] NOT NULL, 4 [Name] [varchar](50) NOT NULL, 5CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 6( 7 [PersonId] ASC 8) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 9) ON [PRIMARY] 10 12--Create the other side of the 1:1 relationship 13CREATE TABLE [dbo].[Contact]( 14 [ContactPersonId] [int] NOT NULL, 15 [ContactNumber] [varchar](30) , 16CONSTRAINT [PK_Contact] PRIMARY KEY CLUSTERED 17( 18 [ContactPersonId] ASC 19) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] 20) ON [PRIMARY] 2122 23-- Example of 1:1 relationship 24-- The contact primary key is the is also a foreign key relationship to the PersonId. 25-- Therefore creating a 1:1 relationship. 26 27ALTER TABLE [dbo].[Contact] WITH CHECK ADD CONSTRAINT [FK_Contact_Person] 28FOREIGN KEY([ContactPersonId]) 29REFERENCES [dbo].[Person] ([PersonId]) 30 GO 创建Contact实体:

1///

2/// Holds a Contact object 3/// which is related to this object through the relation Contact 4/// 5[BindableAttribute()] 6public Contact Contact 7{ 8 get { return entityData.Contact; } 9 set { entityData.Contact = value; } 10} For every Contact entity, since you are working with the actual foreign key, that is your primary key, you would get a PersonId and a PersonIdSource of type Parent.

1private Person _contactPersonIdSource = null; 2 3///

4/// Gets or sets the source 6/// The source Person for ContactPersonId. 7[Browsable(false), BindableAttribute()] 8public virtual Person ContactPersonIdSource 9{ 10 get { return this._contactPersonIdSource; } 11 set { this._contactPersonIdSource = value; } 12 }

索引的作用:

Indexes are not only a powerful feature to help improve your query execution become more performant, it's also used to create handy data access API methods based on those indexes. The important features of an index is that it can contain 1-n columns and has has the ability to be set as a UNIQUE index.

For example, in the Person Table we created above, if we added an index to the Name column, and said that column was Unique. Then in my data access API I would get a method called Person GetByName(string name). If i had not added that the index was unique, then the method would return a collection of type TList.

SQL: 1 CREATE UNIQUE NONCLUSTERED INDEX [IX_Person] ON [dbo].[Person] 2 ( 3 [Name] ASC

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4