使用Code First创建数据模型
2014-02-24 18:45
281 查看
1、声明主键
2、声明必填字段
3、声明允许NULL字段
4、声明字段长度
5、声明特定属性不是数据库中的字段
6、设计模型之间的关联性
using System; using System.ComponentModel.DataAnnotations; namespace MvcGuestbook.Models { public class Guestbook { [Key] public int No{get;set;} ... } }
2、声明必填字段
using System; using System.ComponentModel.DataAnnotations; namespace MvcGuestbook.Models { public class Guestbook { [Key] public int No{get;set;} [Required] public string Name{get;set;} ... } }
3、声明允许NULL字段
using System; using System.ComponentModel.DataAnnotations; namespace MvcGuestbook.Models { public class Guestbook { [Key] public int No{get;set;} [Required] public string Name{get;set;} public DateTime? CreatedOn{get;set;} } }
4、声明字段长度
using System; using System.ComponentModel.DataAnnotations; namespace MvcGuestbook.Models { public class Guestbook { [Key] public int No{get;set;} [Required] [MaxLength(5)] public string Name{get;set;} public DateTime? CreatedOn{get;set;} } }
5、声明特定属性不是数据库中的字段
using System; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace MvcGuestbook.Models { public class Guestbook { [Key] public int No{get;set;} [Required] [MaxLength(5)] public string Name{get;set;} public DateTime? CreatedOn{get;set;} [NotMapped] public string FamilyName{ get{return this.Name.Substring(0,1);} set{this.Name=value.Substring(0,1)+this.Name.Substring(1);} } } }
6、设计模型之间的关联性
using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace MvcGuestbook.Models { public class Guestbook { [Key] public int No{get;set;} public DateTime? CreatedOn{get;set;} public Member Menber{get;set;} } public class Member { [Key] public int No{get;set;} [Required] [MaxLength(5)] public string Name{get;set;} public ICollection<Guestbook> Guestbook{get;set;} } }
相关文章推荐
- 支持“xxxContext”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
- 支持“XX”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
- 支持“xxx”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
- [06] 使用 MVC 5 的 EF6 Code First 入门 系列:为ASP.NET MVC应用程序创建更复杂的数据模型
- MVC中使用EF(1):为ASP.NET MVC程序创建Entity Framework数据模型
- 使用EF Model First创建edmx模型,数据库有数据的情况下,如何同时更新模型和数据库
- Imode平台下使用原生数据创建3D模型_旋转
- 使用Code First 创建映射到现有数据库的基于代码的模型
- ASP.NET MVC3 实例(二) 使用 Entity Framework 创建实体数据模型
- 支持“WeShopDb”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库
- ASP.NET MVC3 实例(二) 使用 Entity Framework 创建实体数据模型
- [01] 使用 MVC 5 的 EF6 Code First 入门 系列:建立一个EF数据模型
- 译文:使用MVC5的Entity Framework 6入门(六)——为ASP.NET MVC应用程序创建更复杂的数据模型
- ASP.NET MVC3 实例(二) 使用 Entity Framework 创建实体数据模型
- 支持“***Context”上下文的模型已在数据库创建后发生更改。请考虑使用 Code First 迁移更新数据库(http://go.microsoft.com/fwlink/?LinkId=238269)。
- Flask系列教程(二)--------------使用SQLAlchemy创建数据模型
- 在 MySQL Workbench 中如何可视的建立 MySQL 物理数据模型,并使用它来创建一个 MySQL 数据库
- ASP.NET MVC3 实例(二) 使用 Entity Framework 创建实体数据模型
- Setting the defaults for database access——使用createTables.php自动创建数据模型
- Entity Framework应用:使用Code First模式管理数据库创建和填充种子数据