using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.ComponentModel.DataAnnotations.Schema;
namespace GTBlog.Model
{
public class EfDbContext:DbContext
{
public EfDbContext()
: base("connStr")
{
}
//增加表后在程序包管理控制台中执行下面的命令
//Add-Migration AddBlogUrl //新增一个数据库迁移版本 AddBlogUrl是要新增版本名称,这个名称必须是唯一的,不能重复
//Update-Database //更新数据库
public DbSet<User> Users { get; set; }
public DbSet<Category> Categories { get; set; }
public DbSet<Article> Articles { get; set; }
public DbSet<DBInfo> DBInfos { get; set; }
public DbSet<Menu> Menus { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Configurations
.Add(new UserConfiguration())
.Add(new CategoryConfiguration())
.Add(new ArticleConfiguration())
.Add(new DBInfoConfiguration())
.Add(new MenuConfiguration());
base.OnModelCreating(modelBuilder);
}
#region UserConfiguration
public class UserConfiguration : EntityTypeConfiguration<User>
{
public UserConfiguration()
{
HasKey(c => c.Id);
Property(c => c.Id)
.IsRequired();
Property(c => c.UserName)
.HasMaxLength();
Property(c => c.Password)
.HasMaxLength();
}
}
#endregion
#region CategoryConfiguration
public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
public CategoryConfiguration()
{
HasKey(c => c.Id);
Property(c => c.Id)
.IsRequired();
Property(c => c.Name)
.IsRequired()
.HasMaxLength();
}
}
#endregion
#region ArticleConfiguration
public class ArticleConfiguration : EntityTypeConfiguration<Article>
{
public ArticleConfiguration()
{
HasKey(c => c.Id);
Property(c => c.Id)
.IsRequired();
Property(c => c.Title)
.HasMaxLength();
Property(c => c.Description)
.HasMaxLength();
}
}
#endregion
#region DBInfoConfiguration
public class DBInfoConfiguration : EntityTypeConfiguration<DBInfo>
{
public DBInfoConfiguration()
{
HasKey(c => c.Id);
Property(c => c.Id)
.IsRequired();
Property(c => c.Explain)
.IsRequired()
.HasMaxLength();
}
}
#endregion
#region MenuConfiguration
public class MenuConfiguration : EntityTypeConfiguration<Menu>
{
public MenuConfiguration()
{
HasKey(c => c.Id);
Property(c => c.Id)
.IsRequired();
Property(c => c.Name)
.HasMaxLength();
Property(c => c.Url)
.HasMaxLength();
}
}
#endregion
}
}