visual studio 2013 Entity Framework 6 MVC 5 体验(一)修改默认数据库,增加自定义字段
来源:互联网 发布:mac book 复制 编辑:程序博客网 时间:2024/05/16 11:34
visual studio 2013于北京时间2013年11月13日23:00时正式发布,整整10天了,该写一篇体验的文章了。
在Views\Home\Index.cshtml文件里替换相关内容。
在控制台中输入Add-Migration "电话"
忘了在顶部增加using System;
废话不说,先新建一个项目
选择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;
保存,运行,效果如下:
- visual studio 2013 Entity Framework 6 MVC 5 体验(一)修改默认数据库,增加自定义字段
- Entity framework 增加默认执行时间
- 如何修改Visual Studio默认数据库localDB
- Visual Studio 2010安装Entity Framework办法
- Visual Studio 2010安装Entity Framework方法
- entity framework 数据库默认时间的问题
- Entity Framework 6源码学习(一)
- Entity Framework(3)修改
- 费下载 !微软 Visual Studio 2013 and ASP.NET MVC 5 新特性体验课程
- Entity Framework(2)数据库
- [Visual Studio & Xamarin] 开发架构(二)-Repository与Entity Framework
- Entity Framework 了解(一)
- Entity Framework(一)基础
- Entity Framework 6 中为数据库操作增加日志记录功能
- MVC 基架不支持 Entity Framework 6
- MVC Entity Framework
- 学习 Entity Framework + MVC
- Visual Studio 2008移动开发初体验(一)
- CentOS安装PostGIS
- system troubleshooting
- SGU104 DP
- linux编译ffmjpg和x264总结
- 常见的各种操作延迟时间
- visual studio 2013 Entity Framework 6 MVC 5 体验(一)修改默认数据库,增加自定义字段
- Ubuntu12.04LTS gcc g++版本设置
- 關於Cross Join 和 Outer/Cross Apply
- ibatis入门示例
- Windows 7 系统 优化、调整、恢复(兼SSD优化)
- C++ 中string.find() 函数的用法总结
- SQL无法启动-TDSSNIClient 初始化失败,出现错误 0x277a,状态代码 0xa
- 小公司技术管理者的点滴--处理和老板的冲突
- linux下md5sum的使用