vs2015 mvc5+ef6+BootStrap (代碼生成數據庫)


隨着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文件。可是每次都要運行下項目才能看待修改過的數據庫,很不方便,不過NuGet帶來了便利。

先要在項目目錄先創建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文件的過程,可以刪掉。

好了,到這一步,小伙伴可打開服務資源管理器,在數據連接看到數據庫了。動手試試吧!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM