EF CodeFirst 创建数据库
- 很多时候,一个项目只有模型层或者某几个模型类,没有数据库脚本,想创建数据库来运行项目,只能一个个敲SQL建表语句或图形化操作,十分耗时间,也很费脑细胞,
- 最近发现可以利用EF 里的自动创建数据库的功能,可以节约不少时间,数据库创建后可能跟实际数据库细节方面有所偏差,但的确节省不少劳力,
- 数据库生成后,我们只需要修改字段类型,删除不必要的表与表之间的关系,添加一部分聚集索引,非聚集索引。
一.首先VS2013以上版本创建MVC4或者MVC5项目,在MVC自动生成的Models文件夹下,把你需要创建数据表的类添加进去,如图:

Blog.cs实体类
[Table("Blog")]
public class Blog
{
public Blog()
{
Tags = new HashSet<Tag>();
Users = new HashSet<User>();
Posts = new HashSet<Post>();
SecurityKey = Guid.NewGuid();
}
public virtual int Id { get; set; }
public virtual Guid SecurityKey { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
public virtual string Title { get; set; }
public virtual string Subtitle { get; set; }
public virtual string LogoImage { get; set; }
public virtual bool AllowsComments { get; set; }
public virtual DateTime CreatedAt { get; set; }
}
Category.cs实体类
[Table("Category")]
public class Category
{
public Category()
{
Posts = new HashSet<Post>();
}
public virtual string Name { get; set; }
public virtual int Id { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
[Table("Comment")]
public class Comment
{
public virtual int Id { get; set; }
public virtual Post Post { get; set; }
public virtual string Name { get; set; }
public virtual string Email { get; set; }
public virtual string HomePage { get; set; }
public virtual int Ip { get; set; }
public virtual string Text { get; set; }
}
Post.cs实体类
[Table("Post")]
public class Post
{
public Post()
{
Tags = new HashSet<Tag>();
Categories = new HashSet<Category>();
Comments = new HashSet<Comment>();
}
public virtual int Id { get; set; }
public virtual Blog Blog { get; set; }
public virtual User User { get; set; }
public virtual string Title { get; set; }
public virtual string Text { get; set; }
public virtual DateTime PostedAt { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
ReadOnlyBlog.cs实体类
[Table("ReadOnlyBlog")]
public class ReadOnlyBlog
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
}
Tag.cs实体类
[Table("Tag")]
public class Tag
{
public virtual int Id { get; set; }
public virtual object Entity { get; set; }
public virtual string Name { get; set; }
}
User.cs实体类
[Table("User")]
public class User
{
public User()
{
Posts = new HashSet<Post>();
Blogs = new HashSet<Blog>();
}
public virtual ICollection<Blog> Blogs { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public virtual int Id { get; set; }
public virtual byte[] Password { get; set; }
public virtual string Email { get; set; }
public virtual string Username { get; set; }
public virtual DateTime CreatedAt { get; set; }
public virtual string Bio { get; set; }
}
建立数据库 上下文,把AccountModels原先的实体删掉,改成如下样子,并且删除AccountController,不然编译通不过
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<Blog> Blog { get; set; }
public DbSet<Category> Category { get; set; }
public DbSet<Comment> Comment { get; set; }
public DbSet<Post> Post { get; set; }
public DbSet<ReadOnlyBlog> ReadOnlyBlog { get; set; }
public DbSet<Tag> Tag { get; set; }
public DbSet<User> User { get; set; }
}
接着,我们在Web.Config里配置一下数据库的连接字符串
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.;Initial Catalog=NHibernate;Persist Security Info=True;User ID=sa;Password=123456" providerName="System.Data.SqlClient" />
</connectionStrings>
最后我们使用HomeController来Index方法来执行生成,注意一定要将全部实体类跑一遍,才能触发生成对应的表

生成的结果如图:

数据关系图:
