标签:
---------------------目录--------------------------
EntityFramework 5.0 CodeFirst 教程03-数据结构的定义/列的属性 (2015-07-22 17:30)
EntityFramework 5.0 CodeFirst 教程02-删除和修改/架构改变异常的处理 (2015-07-14 17:50)
EntityFramework 5.0 CodeFirst 教程01-搭建环境和快速上手 (2015-07-13 10:48)
---------------------目录--------------------------
首先感谢大家的支持,最近比较忙,不过会抽时间讲完Code First的 谢谢大家的支持 想更多的学习资源,可以访问我的主页的,谢谢
这集我们来学习定义一个数据结构,首先看一下.NET类型和SQL类型的对应表,左侧是sql类型,右侧是.NET类型
public class Person { public int PersonId { get; set; } [MaxLength(30)] public string FirstName { get; set; } [MaxLength(30)] public string LastName { get; set; } [StringLength(1, MinimumLength = 1)] [Column(TypeName = "char")] public string MiddleName { get; set; } }
[Column(TypeName = "char")] 表示该列属性是一个char类型
static void Main(string[] args) { //更多资料可以登录本人的主页:www.bamn.cn 欢迎大家 using (var personContext = new Context()) { //如果不存在数据库则创建 bool res = personContext.Database.CreateIfNotExists(); //往数据库中添加数据 var person = new Person { FirstName = "John", LastName = "Doe", MiddleName = "Mid" }; personContext.People.Add(person); personContext.SaveChanges(); //获取数据 var savedPeople = personContext.People; foreach (var p in savedPeople) { Console.WriteLine("Last name:{0},first name:{1},id {2},mid={3}", p.LastName, p.FirstName, p.PersonId,p.MiddleName); } } Console.ReadKey(); }
[MaxLength(30, ErrorMessage = "你的名超过了30个字节")] public string FirstName { get; set; }
class Program { static void Main(string[] args) { //更多资料可以登录本人的主页:www.bamn.cn 欢迎大家 using (var personContext = new Context()) { //如果不存在数据库则创建 bool res = personContext.Database.CreateIfNotExists(); //往数据库中添加数据 var person = new Person { FirstName = "John", LastName = "Doe", MiddleName = "M" }; personContext.People.Add(person); personContext.SaveChanges(); //获取数据 var savedPeople = personContext.People; foreach (var p in savedPeople) { Console.WriteLine("Last name:{0},first name:{1},id {2},mid={3}", p.LastName, p.FirstName, p.PersonId,p.MiddleName); } } Console.ReadKey(); } } public class Person { public int PersonId { get; set; } [MaxLength(30, ErrorMessage = "你的名超过了30个字节")] public string FirstName { get; set; } [MaxLength(30)] public string LastName { get; set; } [StringLength(1, MinimumLength = 1)] [Column(TypeName = "char")] public string MiddleName { get; set; } } public class Context : DbContext { public Context() : base("name=chapter2") { } public DbSet<Person> People { get; set; } }
EntityFramework 5.0 CodeFirst 教程03-数据结构的定义/列的属性
标签:
原文地址:http://www.cnblogs.com/maijin/p/4668070.html