vs2015 mvc5+ef6+bootstrap (Code generation database)

Source: Internet
Author: User

With the vs2015 coming out, the little friends can play with the new stuff.

For vs2015 's new stuff:

public int?;

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

Wait, here is not detailed list, want to know the small partners can go to http://www.admin10000.com/document/5605.html see

(Of course, small partners can directly Baidu vs2015 new features)

Why is mvc5 here with Bootstrap?

Because the powerful vs2015 the MVC5 and bootstrap bound! Before Easyui was tied up, now Easyui more bootstrap small partners.

When the small partners create MVS5 new project will see a models, notice that the class can be generated in the database table to use OH.

[DisplayName ("Division")]     Public classPart {[Key] Public intID {Get;Set; } [DisplayName ("Division name")] [Required ()] Public stringPartName {Get;Set; } [DisplayName ("Partial encoding")] [Required ()] Public stringPartNo {Get;Set; }  Public VirtualIcollection<applicationform> Applicationforms {Get;Set; }  Public VirtualIcollection<customermanager> Customermanagers {Get;Set; }  Public StaticIenumerable<selectlistitem>getpartsselectlist () {vardb =NewCarloandbcontext (); returnDb. Parts.select (p =NewSelectListItem {Text=P.partname, Value=p.id.        ToString ()}); }    }
View Code

The Foreign key table can be this way oh:

 Public classContact { Public intID {Get;Set; } [ForeignKey ("Applicationform")]         Public intApplicationformid {Get;Set; }  Public VirtualApplicationform Applicationform {Get;Set; }  PublicContactType ContactType {Get;Set; } [DisplayName ("name")] [Stringlength ( -)]         Public stringName {Get;Set; } [DisplayName ("Relationship")]         PublicContactslenderrelationship Relation {Get;Set; } [DisplayName ("Mobile Phone")] [Stringlength ( the)]         Public stringMobile {Get;Set; } [Stringlength ( -)] [DisplayName ("Home Phone")]         Public stringHometelephone {Get;Set; } [DisplayName ("are you aware of this loan ?")]         Public BOOLIsknow {Get;Set; } }
View Code

It's OK:

 Public classHouse {[ForeignKey ("Applicationform")]         Public intID {Get;Set; }  Public VirtualApplicationform Applicationform {Get;Set;} [DisplayName ("whether the local")]         Public BOOL? Hashouse {Get;Set; } [DisplayName ("Residential Address")] [Stringlength ( -)]         Public stringliveaddress {Get;Set; } [DisplayName ("whether the local")]         Public BOOL? IsLocal {Get;Set; } [DisplayName ("whether mortgage")]         Public BOOL? Isloan {Get;Set; } [DisplayName ("Buying Time")] [DisplayFormat (dataformatstring="{0:yyyy-mm}", Applyformatineditmode =true)]         PublicDatetime? Buytime {Get;Set; } [DisplayName ("Property Address")] [Stringlength ( -)]         Public stringAddress {Get;Set; } [DisplayName ("Postal Code")] [Stringlength ( -)]         Public stringHousepostcode {Get;Set; } }
View Code

Of course the generated table needs to inherit the next DbContext, but MVC5 will help you get it done.

 Public class Testdbcontext:dbcontext    {        protectedoverridevoid  onmodelcreating (Dbmodelbuilder modelBuilder)        {        }    }

If only the above, the personal feel inconvenient to use, small partners also found it, where is the database?

In fact, the MDF file is generated under the App_Data folder as long as the project is running. But every time to run the project to see the modified database, very inconvenient.

First create the Migrations folder in the project directory and create a class in it:

Internal Sealed classConfiguration:dbmigrationsconfiguration<models.testdbcontext>    {         PublicConfiguration () {automaticmigrationsenabled=true; Contextkey="ScrollTest.Models.TestDBContext"; }        protected Override voidSeed (Models.testdbcontext context) {//This method is 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

This is the time to use the tool-nuget Package Manager-Package Management Console

After opening, first enter

Add-migration Initial

When you are finished, enter

Update-database

This time will be under the App_Data folder to generate MDF files, the attentive little partner will find migrations folder at this moment there is a similar

201511100627099_initial such a file, in fact, it is only a record of the process of generating MDF files, can be deleted.

Well, in this step, the small partner can open the Service explorer and see the database on the data connection. Try it!

vs2015 mvc5+ef6+bootstrap (Code generation database)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.