码迷,mamicode.com
首页 > 数据库 > 详细

vs2015 mvc5+ef6+BootStrap (代码生成数据库)

时间:2015-11-10 16:07:16      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:

随着vs2015出来了,小伙伴们可以玩玩新东东了。

对于vs2015的的新东东:

public int?;

public string Name{get;set;}="Peter"; 

等等,在这里就不详细列举了,想知道的小伙伴可以去http://www.admin10000.com/document/5605.html看看

(当然小伙伴可以直接百度vs2015新特性)

为什么mvc5在这里要和BootStrap放在一起?

因为强大的vs2015把mvc5和BootStrap绑定了!之前easyui被绑了,现在easyui多了BootStrap小伙伴。

当小伙伴们创建mvs5新项目之后会看到有个Models,注意里面的class可以生成数据库里面的Table来用哦。

技术分享
[DisplayName("分部")]
    public class Part
    {
        [Key]
        public int ID { get; set; }
        [DisplayName("分部名称")]
        [Required()]
        public string PartName { get; set; }
        [DisplayName("分部编码")]
        [Required()]        
        public string PartNo { get; set; }

        public virtual ICollection<ApplicationForm> ApplicationForms { get; set; }
        public virtual ICollection<CustomerManager> CustomerManagers { get; set; }
        public static IEnumerable<SelectListItem> GetPartsSelectList()
        {
            var db = new CarLoanDBContext();
            return db.Parts.Select(p => new SelectListItem
            {
                Text = p.PartName,
                Value = p.ID.ToString()
            });
        }
    }
View Code

外键表可以这样哦:

技术分享
public class Contact
    {
        public int ID { get; set; }
        [ForeignKey("ApplicationForm")]
        public int ApplicationFormID { get; set; }
        public virtual ApplicationForm ApplicationForm { get; set; }
        
        public ContactType ContactType { get; set; }
        [DisplayName("姓名")]
        [StringLength(20)]
        public string Name { get; set; }
        [DisplayName("关系")]
        public ContactsLenderRelationship Relation { get; set; }
        [DisplayName("移动电话")]
        [StringLength(15)]
        public string Mobile { get; set; }
        [StringLength(20)]
        [DisplayName("住宅电话")]
        public string HomeTelephone { get; set; }
        [DisplayName("是否知晓此项贷款")]
        public bool IsKnow { get; set; }
    }
View Code

还可以:

技术分享
public class House
    {   
        [ForeignKey("ApplicationForm")]
        public int ID { get; set; }
        public virtual ApplicationForm ApplicationForm { get;set;}

        [DisplayName("是否在本地")]
        public bool? HasHouse { get; set; }
        [DisplayName("居住地址")]
        [StringLength(20)]
        public string LiveAddress { get; set; }

        [DisplayName("是否在本地")]
        public bool? IsLocal { get; set; }

        [DisplayName("是否按揭")]
        public bool? IsLoan { get; set; }

        [DisplayName("购房时间")]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM}", ApplyFormatInEditMode = true)]
        public DateTime? BuyTime { get; set; }

        [DisplayName("房产地址")]
        [StringLength(50)]
        public string Address { get; set; }
        [DisplayName("邮编")]
        [StringLength(20)]
        public string HousePostCode { get; set; }
        
    }
View Code

 

当然生成Table需要继承下DbContext了,不过mvc5会帮你搞定的。

public class TestDBContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }
    }

如果仅仅是以上的话,个人感觉不方便使用的,小伙伴也发现了吧,数据库在哪?

其实只要项目运行就会在App_Data文件夹下面生成mdf文件。可是每次都要运行下项目才能看待修改过的数据库,很不方便。

先要在项目目录先创建Migrations文件夹,在里面创建一个class:

技术分享
internal sealed class Configuration : DbMigrationsConfiguration<Models.TestDBContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true;
            ContextKey = "ScrollTest.Models.TestDBContext";
        }

        protected override void Seed(Models.TestDBContext 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" }
            //    );
            //
        }
    }
View Code

 

这时候就需要使用 工具-NuGet程序包管理器-  程序包管理控制台 

打开之后首先输入  

add-migration Initial

完成后在输入

update-database

这个时候就会在App_Data文件夹下面生成mdf文件了,细心的小伙伴会发现Migrations文件夹这时候有个类似

201511100627099_Initial这样的文件,其实它仅仅是记录生成mdf文件的过程,可以删掉。

好了,到这一步,小伙伴可打开服务资源管理器,在数据连接看到数据库了。动手试试吧!

vs2015 mvc5+ef6+BootStrap (代码生成数据库)

标签:

原文地址:http://www.cnblogs.com/shootingstar/p/4953000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!