示例一

新建一个控制台应用程序,并安装entityframework

新建一个文件Blog.cs类,输入以下代码:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace DataAnnotations
{
public class Blog
{
[Key]
public int PrimaryTrackingKey { get; set; } [Required]
public string Title { get; set; } [MaxLength()]
public string BloggerName { get; set; } [NotMapped]
public string BlogCode
{
get
{
return Title.Substring(, ) + ":" + BloggerName.Substring(, );
}
}
}
}

这个示例就是演示代码中的4个标记,接下来生成DbContext类:

using System.Data.Entity;

namespace DataAnnotations
{
internal class TestContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
}
}

程序的执行文件Program.cs如下:

using System;

namespace DataAnnotations
{
internal class Program
{
private static void Main(string[] args)
{
populate();
retreive();
Console.WriteLine("请按任意键结束!");
Console.ReadKey();
} private static void populate()
{
using (var context = new TestContext())
{
var blog = new Blog { Title = "Hello World! ", BloggerName = "You are freshman." };
context.Blogs.Add(blog);
context.SaveChanges();
}
}//void private static void retreive()
{
using (var context = new TestContext())
{
foreach (var blog in context.Blogs)
{
Console.WriteLine("{0}.Title = {1}, BloggerName = {2}", blog.PrimaryTrackingKey, blog.Title, blog.BloggerName);
}
}
}//void
}
}

执行程序,并按任意键终止!

可以在其生成的数据库中找到Blogs表,表的创建代码如下:

CREATE TABLE [dbo].[Blogs] (
[PrimaryTrackingKey] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (MAX) NOT NULL,
[BloggerName] NVARCHAR (50) NULL,
CONSTRAINT [PK_dbo.Blogs] PRIMARY KEY CLUSTERED ([PrimaryTrackingKey] ASC)
);

此代码和Blog.cs代码对照,可以知道每个标记的含义。[Key]是PRIMARY KEY,[Required]是NOT NULL,[MaxLength(50)]中的数字对应NVARCHAR(50)中的数字,[NotMapped]表示这个属性不用保存在数据库中。

这里要说明的是,整数类型的主键默认identity(1,1), string类型默认映射为nvarchar(max)。

示例二

这个示例演示复合主键与复合外键:

模型文件为:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace DataAnnotations
{
public class Passport
{
[Key]
[Column(Order = )]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int PassportNumber { get; set; } [Key]
[Column(Order = )]
public string IssuingCountry { get; set; } public DateTime Issued { get; set; }
public DateTime Expires { get; set; }
} public class PassportStamp
{
[Key]
public int StampId { get; set; } public DateTime Stamped { get; set; }
public string StampingCountry { get; set; } [ForeignKey(name: "Passport")]
[Column(Order = )]
public int PassportNumber { get; set; } [ForeignKey(name: "Passport")]
[Column(Order = )]
public string IssuingCountry { get; set; } public Passport Passport { get; set; }
}
}

生成的数据库表的代码为:

CREATE TABLE [dbo].[Passports] (
[PassportNumber] INT NOT NULL,
[IssuingCountry] NVARCHAR (128) NOT NULL,
[Issued] DATETIME NOT NULL,
[Expires] DATETIME NOT NULL,
CONSTRAINT [PK_dbo.Passports] PRIMARY KEY CLUSTERED ([PassportNumber] ASC, [IssuingCountry] ASC)
); CREATE TABLE [dbo].[PassportStamps] (
[PassportNumber] INT NOT NULL,
[IssuingCountry] NVARCHAR (128) NULL,
[StampId] INT IDENTITY (1, 1) NOT NULL,
[Stamped] DATETIME NOT NULL,
[StampingCountry] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_dbo.PassportStamps] PRIMARY KEY CLUSTERED ([StampId] ASC),
CONSTRAINT [FK_dbo.PassportStamps_dbo.Passports_PassportNumber_IssuingCountry] FOREIGN KEY ([PassportNumber], [IssuingCountry]) REFERENCES [dbo].[Passports] ([PassportNumber], [IssuingCountry])
); GO
CREATE NONCLUSTERED INDEX [IX_PassportNumber_IssuingCountry]
ON [dbo].[PassportStamps]([PassportNumber] ASC, [IssuingCountry] ASC);

复合键的Order是按着大小排序,并不是指序号。外键的顺序要与主键对应列位置一致。Order值不一定要相等。

代码中使用[DatabaseGenerated(DatabaseGeneratedOption.None)]关闭了整数类型主键的Identity特性。另外两个选项为Computed,Identity。

示例三

模型代码

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; namespace DataAnnotations
{
public class Blog
{
public int BlogId { get; set; } [Required]
public string Title { get; set; } public BlogDetails BlogDetail { get; set; }
} [ComplexType]
public class BlogDetails
{
public DateTime? DateCreated { get; set; } [MaxLength()]
public string Description { get; set; }
}
}

没有主键,又没有引用其他实体的类为ComplexType,这里的标记是可以省略的。上下文代码

using System.Data.Entity;

namespace DataAnnotations
{
internal class TestContext : DbContext
{
public DbSet<Blog> Blogs { get; set; } }
}

上下文中只有Blog集合,没有细节集合。填充与读取代码

        private static void populate()
{
using (var context = new TestContext())
{
var blog = new Blog
{
Title = "My First",
BlogDetail = new BlogDetails
{
DateCreated = DateTime.Now,
Description = "这里省略10000字!"
}
}; context.Blogs.Add(blog);
context.SaveChanges();
}
}//void private static void retreive()
{
using (var context = new TestContext())
{
foreach (var b in context.Blogs)
{
Console.WriteLine("{0}.{1}", b.BlogId, b.Title); var d = b.BlogDetail;
Console.WriteLine("{0}.{1}", d.DateCreated, d.Description);
}
}
}//void

数据库表

CREATE TABLE [dbo].[Blogs] (
[BlogId] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (MAX) NOT NULL,
[BlogDetail_DateCreated] DATETIME NULL,
[BlogDetail_Description] NVARCHAR (250) NULL,
CONSTRAINT [PK_dbo.Blogs] PRIMARY KEY CLUSTERED ([BlogId] ASC)
);

复杂类型将与引用他的实体一起扁平到一个数据库表中,默认的列名为ComplexTypeName_PropertyName。

示例四

本例演示如何为表和表中的列改名,修改列的数据类型,模型:

using System.ComponentModel.DataAnnotations.Schema;

namespace DataAnnotations
{
[Table("InternalBlogs", Schema = "dbo")]
public class Blog
{
public int BlogId { get; set; } [Column("BlogDescription", TypeName = "ntext")]
public string Description { get; set; }
}
}

对应的数据库表为:

CREATE TABLE [dbo].[InternalBlogs] (
[BlogId] INT IDENTITY (1, 1) NOT NULL,
[BlogDescription] NTEXT NULL,
CONSTRAINT [PK_dbo.InternalBlogs] PRIMARY KEY CLUSTERED ([BlogId] ASC)
);

可见,表的名称,列的名称与数据类型都发生的改变。

示例五

数据库通过并发检查和行版本来处理并发,模型代码为:

using System.ComponentModel.DataAnnotations;

namespace DataAnnotations
{
public class Blog
{
public int BlogId { get; set; } [ConcurrencyCheck]
public string BloggerName { get; set; } [Timestamp]
public Byte[] TimeStamp { get; set; }
}
}

对应生成的数据库表代码为:

CREATE TABLE [dbo].[Blogs] (
[BlogId] INT IDENTITY (1, 1) NOT NULL,
[BloggerName] NVARCHAR (MAX) NULL,
[TimeStamp] ROWVERSION NOT NULL,
CONSTRAINT [PK_dbo.Blogs] PRIMARY KEY CLUSTERED ([BlogId] ASC)
);

[Timestamp]对应生成了非空的行版本类型,标记了[ConcurrencyCheck]的BloggerName属性会在生成的更新数据库代码的Where子句中添加并发检查。

When SaveChanges is called, because of the ConcurrencyCheck annotation on the BloggerName field, the original value of that property will be used in the update. The command will attempt to locate the correct row by filtering not only on the key value but also on the original value of BloggerName.

CodeFirst建模:DataAnotation的更多相关文章

  1. 《Entity Framework 6 Recipes》中文翻译系列 (41) ------ 第七章 使用对象服务之标识关系中使用依赖实体与异步查询保存

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-7  标识关系中使用依赖实体 问题 你想在标识关系中插入,更新和删除一个依赖实体 ...

  2. 《Entity Framework 6 Recipes》中文翻译系列 (33) ------ 第六章 继承与建模高级应用之TPH与TPT (2)

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-8  嵌套的TPH建模 问题 你想使用超过一层的TPH继承映射为一张表建模. 解 ...

  3. Rafy 领域实体框架 - 领域模型设计器(建模工具)设计方案

    去年4月,我们为 Rafy 框架添加了领域模型设计器组件.时隔一年,谨以本文,简要说明该领域模型设计器的设计思想. 设计目标 Rafy 实体框架中以领域驱动设计作为指导思想.所以在开发时,以领域建模为 ...

  4. CodeFirst 的编程方式

    第一步:创建控制台项目第二步:添加新建项目→Ado.Net空实体模型第三步:添加实体:Customer,添加几个必要的测试字段第四步:添加实体之间的联系第五步:根据模型生成数据库脚本,并执行sql脚本 ...

  5. CodeFirst 初恋

    CodeFirst 初恋 原著:Prorgamming Entity Framework Entitywork Code First 大家好! 我是AaronYang,这本书我也挺喜欢的,看了一半了, ...

  6. Entity Framework——建模建库

    1数据库初始化策略选择 三种初始化策略: 1)CreateDatabaseIfNotExists:默认的策略.如果数据库不存在,那么就创建数据库.但是如果数据库已存在,而且实体发生了变化,就会出现异常 ...

  7. 【AaronYang风格】第一篇 CodeFirst 初恋

             原著:Prorgamming Entity Framework Entitywork Code First 大家好! 我是AaronYang,这本书我也挺喜欢的,看了一半了,今晚也没 ...

  8. EntityFramework 基本模式和Code-First的简单使用

    1.Database-First  Database First就是首先建立好数据库,或者存在现成的数据库也可以.然后在vs中添加ADO.Net实体数据模型,找到需要的数据库和表.它是以数据库设计为基 ...

  9. MVC5+EF6+MYSQl,使用codeFirst的数据迁移

    之前本人在用MVC4+EF5+MYSQL搭建自己的博客.地址:www.seesharply.com;遇到一个问题,就是采用ef的codefirst模式来编写程序,我们一般会在程序开发初期直接在glob ...

随机推荐

  1. 根据Dockerfile创建hello docker镜像

    一.编写hello可执行c文件: 1.安装:gcc glibc glibc-static yum install -y gcc glibc glibc-static 2.编写hello.c:vim h ...

  2. js 技巧 (九)按键JS

    1. 禁止复制(copy),禁用鼠标右键! <SCRIPT> //加入页面保护 function rf() {return false; } document.oncontextmenu ...

  3. CC3200 TI 笔记

    I2C I2C总线是由Philips公司开发的一种简单.双向二线制同步串行总线.它只需要两根线即可在连接于总线上的器件之间传送信息. I2S I2S(Inter-IC Sound)总线, 又称 集成电 ...

  4. HDU 2147 找规律博弈

    题目大意: 从右上角出发一直到左下角,每次左移,下移或者左下移,到达左下角的人获胜 到达左下角为必胜态,那么到达它的所有点都为必败态,每个点的局势都跟左,下,左下三个点有关 开始写了一个把所有情况都计 ...

  5. [NOIP2006] 提高组 洛谷P1063 能量项链

    题目描述 在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并且,对于相邻的两颗珠子,前一颗珠子的尾标记一定 ...

  6. PatentTips - Universal RAID Class Driver

    BACKGROUND OF THE INVENTION The present invention relates to the field of data storage devices. Comp ...

  7. Devu and Flowers lucas定理+容斥原理

    Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...

  8. Rust 1.7.0 macro宏的复用 #[macro_use]的使用方法

    Rust 1.7.0 中的宏使用范围包含三种情况: 第一种情况是宏定义在当前文件里.这个文件可能是 crate 默认的 module,也可能是随意的 module 模块. 另外一种情况是宏定义在当前 ...

  9. Setting up Storm and Running Your First Topology

    http://www.haroldnguyen.com/blog/2015/01/setting-up-storm-and-running-your-first-topology/ --------- ...

  10. cheat sheet (小抄的意思-考试的时候,带在路上原先抄的重要的知识点)

    Cheat Sheet,这里面有个Cheat(欺骗),想当然的话,意思肯定不好.事实上,这Cheat Sheet 的原意的确也就是“小抄”的意思.所以,字典的定义是:“A piece of paper ...