visual studio 2013 Entity Framework 6 MVC 5 体验(一)修改默认数据库,增加自定义字段

来源:互联网 发布:mac book 复制 编辑:程序博客网 时间:2024/05/16 11:34
visual studio 2013于北京时间2013年11月13日23:00时正式发布,整整10天了,该写一篇体验的文章了。

废话不说,先新建一个项目

选择MVC,确定

打开 Views\Shared\_Layout.cshtml文件,按自己的要求修改

<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>    <meta charset="utf-8" />    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>@ViewBag.Title - 我的 ASP.NET 应用程序</title>    @Styles.Render("~/Content/css")    @Scripts.Render("~/bundles/modernizr")</head><body>    <div class="navbar navbar-inverse navbar-fixed-top">        <div class="container">            <div class="navbar-header">                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">                    <span class="icon-bar"></span>                    <span class="icon-bar"></span>                    <span class="icon-bar"></span>                </button>                @Html.ActionLink("应用程序名称", "Index", "Home", null, new { @class = "navbar-brand" })            </div>            <div class="navbar-collapse collapse">                <ul class="nav navbar-nav">                    <li>@Html.ActionLink("主页", "Index", "Home")</li>                    <li>@Html.ActionLink("关于", "About", "Home")</li>                    <li>@Html.ActionLink("联系方式", "Contact", "Home")</li>                </ul>                @Html.Partial("_LoginPartial")            </div>        </div>    </div>    <div class="container body-content">        @RenderBody()        <hr />        <footer>            <p>© @DateTime.Now.Year - 我的 ASP.NET 应用程序</p>        </footer>    </div>    @Scripts.Render("~/bundles/jquery")    @Scripts.Render("~/bundles/bootstrap")    @RenderSection("scripts", required: false)</body></html>

Views\Home\Index.cshtml文件里替换相关内容。

打开web.config文件,修改默认数据库连接字符串

<?xml version="1.0" encoding="utf-8"?><!--  有关如何配置 ASP.NET 应用程序的详细信息,请访问  http://go.microsoft.com/fwlink/?LinkId=301880  --><configuration>  <configSections>    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  </configSections>  <connectionStrings>    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-PingShuo-20131123102758.mdf;Initial Catalog=aspnet-PingShuo-20131123102758;Integrated Security=True"      providerName="System.Data.SqlClient" />  </connectionStrings>  <appSettings>    <add key="webpages:Version" value="3.0.0.0" />    <add key="webpages:Enabled" value="false" />    <add key="ClientValidationEnabled" value="true" />    <add key="UnobtrusiveJavaScriptEnabled" value="true" />  </appSettings>  <system.web>    <authentication mode="None" />    <compilation debug="true" targetFramework="4.5.1" />    <httpRuntime targetFramework="4.5.1" />  </system.web>  <system.webServer>    <modules>      <remove name="FormsAuthenticationModule" />    </modules>  </system.webServer>  <runtime>    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">      <dependentAssembly>        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />      </dependentAssembly>      <dependentAssembly>        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />      </dependentAssembly>      <dependentAssembly>        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />      </dependentAssembly>      <dependentAssembly>        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />      </dependentAssembly>    </assemblyBinding>  </runtime>  <entityFramework>    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">      <parameters>        <parameter value="v11.0" />      </parameters>    </defaultConnectionFactory>    <providers>      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />    </providers>  </entityFramework></configuration>
保存web.config,运行程序,点击注册,注册一个新用户,以激活数据库。

现在打开本机的MSSMS或VS的服务器资源管理器,可以看到已经建好的数据库PS,显示数据,可以看到已经注册的用户。

进行到这里,干脆顺便完善一下用户信息。以建好的模板只有用户名和密码,实际使用中我们可能还需要其他信息,比如我将添加电话、所在部门等。

首先打开程序包管理控制台:


在控制台中输入“Enable-Migrations“,完成了迁移

打开Models\IdentityModels.cs文件,增加以下代码

using Microsoft.AspNet.Identity.EntityFramework;//添加引用using System;namespace PingShuo.Models{    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.    public class ApplicationUser : IdentityUser    {        //添加自定义的用户信息字段        public string 电话 { get; set; }        public string 部门 { get; set; }    }    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>    {        public ApplicationDbContext()            : base("DefaultConnection")        {        }    }}

在控制台中输入Add-Migration "电话"

接着输入:Update-Database

接着输入:Add-Migration "部门"和Update-Database

现在检查数据库,已经多了这两个字段

打开AccountViewModels.cs文件,增加红色部分:

using System.ComponentModel.DataAnnotations;namespace PingShuo.Models{    public class ExternalLoginConfirmationViewModel    {        [Required]        [Display(Name = "用户名")]        public string UserName { get; set; }    }    public class ManageUserViewModel    {        [Required]        [DataType(DataType.Password)]        [Display(Name = "当前密码")]        public string OldPassword { get; set; }        [Required]        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]        [DataType(DataType.Password)]        [Display(Name = "新密码")]        public string NewPassword { get; set; }        [DataType(DataType.Password)]        [Display(Name = "确认新密码")]        [Compare("NewPassword", ErrorMessage = "新密码和确认密码不匹配。")]        public string ConfirmPassword { get; set; }    }    public class LoginViewModel    {        [Required]        [Display(Name = "用户名")]        public string UserName { get; set; }        [Required]        [DataType(DataType.Password)]        [Display(Name = "密码")]        public string Password { get; set; }        [Display(Name = "记住我?")]        public bool RememberMe { get; set; }    }    public class RegisterViewModel    {        [Required]        [Display(Name = "用户名")]        public string UserName { get; set; }        [Required]        [StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]        [DataType(DataType.Password)]        [Display(Name = "密码")]        public string Password { get; set; }        [DataType(DataType.Password)]        [Display(Name = "确认密码")]        [Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]        public string ConfirmPassword { get; set; }        //扩展类的字段        [Required]        [Display(Name = "电话")]        public string 电话 { get; set; }        [Required]        [Display(Name = "部门")]        public string 部门 { get; set; }        //顺便编写一个AppliationUser类的实例,以便后用:        public ApplicationUser GetUser()        {            var user = new ApplicationUser()            {                UserName = this.UserName,                部门 = this.部门,            };            return user;        }    }}

忘了在顶部增加using System;

保存,运行,效果如下:




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 摩托罗拉电话静音了怎么办 对讲机话筒坏了怎么办 摩托罗拉xt1570费电怎么办 主板没有rgb接口怎么办 对讲机频段没了怎么办 怀孕查出宫颈囊怎么办 办养殖场没地怎么办 宝宝睡觉衣服湿透怎么办 开衫衣服往下滑怎么办 迷你世界没有牛怎么办 火龙果树烂了怎么办 误喝鸵鸟墨水怎么办 被鸵鸟啄伤怎么办 被鸵鸟啄住怎么办 鸟翅膀受伤了怎么办 北京卖房户口怎么办 深户挂人才市场集体户小孩怎么办? 郑州房子限购怎么办 外地怎么送东西怎么办 晚上登华山下雨怎么办 北京摇不到号想买车怎么办 北京买车摇不到号怎么办 朋友代购贵了怎么办 赢时通把车开走怎么办 杭州房子租不起怎么办 质量效应2吵架怎么办 学生有两个学籍怎么办 孩子出现双学籍怎么办 在外地读书学籍怎么办 上高中没学籍怎么办 电信欠费了网络怎么办 头发里长脓包怎么办 工作总是做不好怎么办 能力差的人该怎么办 洗衣机里有味道怎么办 洗衣机里面有味道怎么办 洗衣机内有味道怎么办 洗衣机总有味道怎么办 上体育课脚扭伤怎么办 教学实践评价表怎么办 初中孩子成绩下降怎么办