(window.webpackJsonp=window.webpackJsonp||[]).push([[1053],{1461:function(t,e,a){"use strict";a.r(e);var n=a(31),s=Object(n.a)({},(function(){var t=this,e=t.$createElement,a=t._self._c||e;return a("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[a("h1",{attrs:{id:"entity-framework-code-first"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#entity-framework-code-first"}},[t._v("#")]),t._v(" Entity Framework Code First")]),t._v(" "),a("h2",{attrs:{id:"connect-to-an-existing-database"}},[a("a",{staticClass:"header-anchor",attrs:{href:"#connect-to-an-existing-database"}},[t._v("#")]),t._v(" Connect to an existing database")]),t._v(" "),a("p",[t._v("To achieve the simplest task in Entity Framework - to connect to an existing database "),a("code",[t._v("ExampleDatabase")]),t._v(" on your local instance of MSSQL you have to implement two classes only.")]),t._v(" "),a("p",[t._v("First is the entity class, that will be mapped to our database table "),a("code",[t._v("dbo.People")]),t._v(".")]),t._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[t._v("\n class Person\n {\n public int PersonId { get; set; }\n public string FirstName { get; set; }\n }\n\n")])])]),a("p",[t._v("The class will use Entity Framework's conventions and map to table "),a("code",[t._v("dbo.People")]),t._v(" which is expected to have primary key "),a("code",[t._v("PersonId")]),t._v(" and a varchar(max) property "),a("code",[t._v("FirstName")]),t._v(".")]),t._v(" "),a("p",[t._v("Second is the context class which derives from "),a("code",[t._v("System.Data.Entity.DbContext")]),t._v(" and which will manage the entity objects during runtime, pupulate them from database, handle concurrency and save them back to the database.")]),t._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[t._v("\n class Context : DbContext\n {\n public Context(string connectionString) : base(connectionString)\n {\n Database.SetInitializer(null);\n }\n\n public DbSet People { get; set; }\n }\n\n")])])]),a("p",[t._v("Please mind, that in the constructor of our context we need to set database initializer to null - we don't want Entity Framework to create the database, we just want to access it.")]),t._v(" "),a("p",[t._v("Now you are able manipulate data from that table, e.g. change the "),a("code",[t._v("FirstName")]),t._v(" of first person in the database from a console application like this:")]),t._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[t._v('\n class Program\n {\n static void Main(string[] args)\n {\n using (var ctx = new Context("DbConnectionString"))\n {\n var firstPerson = ctx.People.FirstOrDefault();\n if (firstPerson != null) {\n firstPerson.FirstName = "John";\n ctx.SaveChanges();\n }\n }\n }\n }\n\n')])])]),a("p",[t._v('In the code above we created instance of Context with an argument "DbConnectionString". This has to be specified in our '),a("code",[t._v("app.config")]),t._v(" file like this:")]),t._v(" "),a("div",{staticClass:"language- extra-class"},[a("pre",{pre:!0,attrs:{class:"language-text"}},[a("code",[t._v('\n \n \n \n\n')])])])])}),[],!1,null,null,null);e.default=s.exports}}]);