@{ viewbag.title = "Home page";} <div class= "Jumbotron" >
Run and look at the effect.
Installing EF6Creating a data ModelUnder the Models folder, create Contact.cs, Enrollment.cs, Group.cs three classes respectively
Using system;using system.collections.generic;using system.linq;using system.web;namespace PCT. contact.models{public class "contact" {public int ID {get; set;} public string Name {get; set;} Public DateTime enrollmentdate {get; set;} Public virtual icollection<enrollment> enrollments {get; set;}} }
Using system;using system.collections.generic;using system.linq;using system.web;namespace PCT. contact.models{public class enrollment {public int Enrollmentid {get; set;} public int ContactID {get; set;} public int GroupID {get; set;} Public virtual contact Contact {get; set;} Public virtual group group {get; set;}} }
Using system;using system.collections.generic;using system.linq;using system.web;namespace PCT. contact.models{public enum GroupName { Friend, Family, colleague, schoolmate, stranger } public class Group {public int GroupID {get; set;} Public GroupName? GroupName {get; set;} Public virtual icollection<enrollment> enrollments {get; set;}} }
PS: found that the VS has an automatic hint reference, is not very convenient AH
CreateDatabase Context In the PCT. Create a new folder DAL (Data Access Layer)under the contact item, and continue with the new CommunicationContext.cs
Tragedy Ah, because the class contact and project name contact duplicate, have to write full name Ah, later attention.
Continue to create CommunicationInitializer.cs under the DAL directory
To notify EF to use the initializer class you created, add the entityframework node to the project's Web. config
<entityFramework> <contexts> <context type= "PCT.Contact.DAL.CommunicationContext, PCT. Contact "> <databaseinitializer type=" PCT.Contact.DAL.CommunicationInitializer, PCT. Contact "/> </context> </contexts> <defaultconnectionfactory type=" System.Data.Entity.Infrastructure.SqlConnectionFactory, entityframework "/> <providers> < Provider invariantname= "System.Data.SqlClient" type= "System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver "/> </providers> </entityFramework>
Set upEFUseSQL Server Express LocalDB databaseAdd connectionstrings(above appSettings ) in Project Web. config
<connectionStrings> <add name= "Communicationcontext" connectionstring= "Data source= (LocalDb) \v11.0;i Nitial catalog=contactcommunication;integrated security=sspi; "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>
Create ContactThe Controller and view
Run resultsViewLocalDBThis article the original length
This article original address: http://panchunting.cnblogs.com/
MVC5 + EF6 Simple Example