mvc5+ef6+Bootstrap 项目心得--创立之初

 

1.mvc5+ef6+Bootstrap 项目心得--创立之初

2.mvc5+ef6+Bootstrap 项目心得--身份验证和权限管理

3.mvc5+ef6+Bootstrap 项目心得--WebGrid

   在使用ef6的时候,如果木有系统的学习,很多人都是一脸蒙的吧。经过N天的摸索和网上查找资料学习,总结了下。这里简单说一下ef+bootstrap的安装和使用。

  打开vs2015创建web项目,选择Empty,而不是MVC。然后下面有三个勾选项:Web Froms,MVC,Web Application。把MVC的勾选项。然后会看到项目里面只有ControllerS,Models,Views 三个空的文件夹。

  安装ef6的步骤:

  1.   工具——NuGet包管理器——程序包管理控制平台。
  2. 输入“Install-Package EntityFramework” ——回车,等待安装好
  3. 输入“Install-Package bootstrap”

需要用到的2个模块安装好了。接下来就是使用code first里面的Migrations。

1. 在Models下创建一个Class作为数据库的Table用。

2.继承DbContext。

public class LifeMContext : DbContext
    {
        public LifeMContext() : base("LifeMContext")
        {

        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public DbSet<User> UserList { get; set; }
        public DbSet<AddressInfo> AddressInfoList { get; set; }
        public DbSet<EnumDictionary> EnumDictionaryList { get; set; }
    }

3.记得要在web.config里面加上数据库链接

<connectionStrings>
    <add name="LifeMContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LifeM.mdf;Initial Catalog=LifeM;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

4.在程序包管理控制平台输入“enable-migrations -contexttypename LifeManager.Models.LifeMContext”。“enable-migrations -contexttypename” 是命令创建migrations文件夹和Configuration.cs用的。注意是“enable-migrations -contexttypename” 空格不能少。

internal sealed class Configuration : DbMigrationsConfiguration<LifeManager.Models.LifeMContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
            ContextKey = "LifeManager.Models.LifeMContext";
        }

        protected override void Seed(LifeManager.Models.LifeMContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //以下插入数据会出现中文乱码,VS2015需升级update2
            //var guid = Guid.NewGuid().ToString();
            //context.EnumDictionaryList.AddOrUpdate(t=>t.Value, 
            //new EnumDictionary() { GuidKey = guid, Value = "系统管理", Url = "#", ParentGuid = "0", OrderASC = 1, Level = 1 },
            //new EnumDictionary() { GuidKey = Guid.NewGuid().ToString(), Value = "用户管理", Url = "#", ParentGuid = guid, OrderASC = 1, Level = 2 },
            //new EnumDictionary() { GuidKey = Guid.NewGuid().ToString(), Value = "机构管理", Url = "#", ParentGuid = guid, OrderASC = 2, Level = 3 },
            //new EnumDictionary() { GuidKey = Guid.NewGuid().ToString(), Value = "角色管理", Url = "#", ParentGuid = guid, OrderASC = 3, Level = 4 },
            //new EnumDictionary() { GuidKey = Guid.NewGuid().ToString(), Value = "区域管理", Url = "#", ParentGuid = guid, OrderASC = 4, Level = 5 },
            //new EnumDictionary() { GuidKey = Guid.NewGuid().ToString(), Value = "系统安全", Url = "#", ParentGuid = "0", OrderASC = 1, Level = 5 });
            //context.SaveChanges();
        }
    }

5.在程序包管理控制平台输入“add-migration initial”,等待在输入“update database”。这个时候在项目中有个App_Data文件夹,里面就是数据库啦。之后在Models下创建class或者修改class后,使用“add-migration initial” “update database”2条命令即可更新数据库。注意数据库里面有个“__MigrationHistory”表 一定不能删,一定不能删,一定不能删。后果自己试试就知道了- -!

 

如果觉得敲命令麻烦可以使用界面管理NuGet。右键项目——管理NuGet程序包。直接输入“EntityFramework”,“Bootstrap”,下载就好了。需要加NPOI,JSON,Jquery UI等等都可以在这里搜索,安装。

 

如果觉得文章不错,请给个推荐(◕ˇ◞◟ˇ◕),谢谢。

posted @ 2016-11-17 11:11  丰叔(◕ˇ◞◟ˇ◕)  阅读(1573)  评论(0编辑  收藏  举报