首页 > 代码库 > [转]在ASP.NET MVC3中使用EFCodeFirst 1.0
[转]在ASP.NET MVC3中使用EFCodeFirst 1.0
本文转自:http://kb.cnblogs.com/page/97003/
作者: NinoFocus 来源: 博客园 发布时间: 2011-04-12 10:41 阅读: 11971 次 推荐: 11 原文链接 [收藏]
1. 新建项目
打开VS2010,选择 文件>新建>项目,新建ASP.NET MVC3 Web 应用程序,我这里把它命名为Blog。
2. 编写实体类
对于一个博客,一下几个类应该是必须的吧:
- Post 博客文章类
- Comment 文章评论类,和Post是一对多的关系
- Category 目录类,和Post是一对多的关系
- Tag 标签类,和Post是多对多的关系
- FriendLink 友情链接类
先不考虑管理员之类的东西。 在Model中依次添加上面的类。
namespace Blog.Models { public class Post { public int ID { get; set; } public int CategoryID { get; set; }
public string Title { get; set; } public string Summary { get; set; } public string Alias { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; }
public Category Category { get; set; } public ICollection<Tag> Tags { get; set; } public ICollection<Comment> Coments { get; set; } } }
namespace Blog.Models { public class Comment { public int ID { get; set; } public int PostID { get; set; } public int Level { get; set; } public int ReplyTo { get; set; }
public string UserName { get; set; } public string Email { get; set; } public string Website { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; }
} }
namespace Blog.Models { public class Category { public int ID { get; set; }
public string Name { get; set; } public string Alias { get; set; } public string Description { get; set; } public DateTime CreateTime { get; set; }
public ICollection<Post> Posts { get; set; } } }
namespace Blog.Models { public class Tag { public int ID { get; set; }
public string Name { get; set; } public string Alias { get; set
public string Title { get; set; } public string Summary { get; set; } public string Alias { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; }
public Category Category { get; set; } public ICollection<Tag> Tags { get; set; } public ICollection<Comment> Coments { get; set; } } }
namespace Blog.Models { public class Comment { public int ID { get; set; } public int PostID { get; set; } public int Level { get; set; } public int ReplyTo { get; set; }
public string UserName { get; set; } public string Email { get; set; } public string Website { get; set; } public string Content { get; set; } public DateTime CreateTime { get; set; }
} }
namespace Blog.Models { public class Category { public int ID { get; set; }
public string Name { get; set; } public string Alias { get; set; } public string Description { get; set; } public DateTime CreateTime { get; set; }
public ICollection<Post> Posts { get; set; } } }
namespace Blog.Models { public class Tag { public int ID { get; set; }
public string Name { get; set; } public string Alias { get; set