Before you start, take a look at the official interpretation of the Entity Framework: The Entity Framework (EF) is a object-relational mapper that enables. NET Devel Opers to work with relational data using domain-specific objects. It eliminates the need for most of the data-access code, developers usually need to write. This involves a few key points of knowledge: 1 First it is an object-relational mapping Shoot. 2 Second, it uses domain-specific objects. 3 It rejects the traditional way of accessing the database through SQL statements. In fact, by comparing LINQ to SQL we will find that they are almost identical. Because human thought is more accustomed to the use of object-oriented approach, because it is easier to understand and more convenient to use.
Here's an introduction to how to add the ADO Entity Data Model to VS2015.
Figure one adding the ADO Entity Data Model
1 Create a new folder and add an ADO Entity Data model to this folder, such as Modeltest.
2 Add a database from the EF designer, there are four options, each one corresponds to a model content, here for the time being not introduced, in the subsequent sequence in the introduction.
Figure II Selection of the model content
3 Create a new connection.
Figure three new connections
Figure Four Selecting a data source
Figure V Modifying connection properties
4 Select a database object and produce an object-relational mapping.
Figure six selecting database objects and Settings
Figure seven a domain-specific object produced
Let's take a look at the automatically generated connection string in the next section.
<add name= "dvapentities" connectionstring= "Metadata=res://*/ef6. Modeltest.csdl|res://*/ef6. Modeltest.ssdl|res://*/ef6. Modeltest.msl;provider=mysql.data.mysqlclient;provider connection string= "Server=localhost;user id=root;password= 12345;database=dvap "" Providername= "System.Data.EntityClient"/>
Sometimes we don't just need to configure in the configuration file, so we expose a lot of important information, and in many cases we need to generate the connection string through the code, so what should we do? Because this configuration file still contains a lot of information, after many of my efforts, finally found on MSDN on the answer.
String server = system.configuration.configurationmanager.appsettings["MySQLServer"]; if (!string. IsNullOrEmpty (server)) {string providerName = "MySql.Data.MySqlClient"; Mysqlconnectionstringbuilder Sqlbulider = new Mysqlconnectionstringbuilder (); Sqlbulider. Server = server; Sqlbulider. UserID = "root"; Sqlbulider. Password = "12345"; Sqlbulider. Database = "DVAP"; Sqlbulider. Allowzerodatetime = true; Sqlbulider. Convertzerodatetime = true; Sqlbulider. IntegratedSecurity = true; Entityconnectionstringbuilder Entitybuilder = new Entityconnectionstringbuilder (); Entitybuilder.provider = ProviderName; entitybuilder.providerconnectionstring = Sqlbulider. ToString (); Entitybuilder.metadata = @ "Res://*/ef6. Dvapregularmodel.csdl|res://*/ef6. Dvapregularmodel.ssdl|res://*/ef6. DvapregulaRMODEL.MSL "; m_connectionstring = Entitybuilder.tostring (); }
By the way we can generate the correct connection string, we need to pass the connection string to the auto-generated Dvapentities object after the connection string is generated, and some code is also posted here.
namespace testef6.ef6{using System; Using System.Data.Entity; Using System.Data.Entity.Infrastructure; Public partial class Dvapentities:dbcontext {public dvapentities (): Base ("Name=dvapentities") {} protected override void Onmodelcreating (Dbmodelbuilder modelBuilder) {throw n EW unintentionalcodefirstexception (); } public virtual dbset<dvap_scene_business> dvap_scene_business {get; set;} Public virtual dbset<echarts_barcharts_singleverticalcolumn> Echarts_barcharts_singleverticalcolumn {get; set ; } public Virtual dbset<echarts_barcharts_singleverticalcolumnproperty> Echarts_barcharts_singleverticalcolum Nproperty {get; set;} Public virtual dbset<logs> logs {get; set;} Public virtual dbset<options> Options {get; set;} Public virtual dbset<test_stackhistogram> Test_stackhistogram {get; set;} Public Virtual dbset<users> users {get; set;} }}
Simply using the default Dvapentities function is not a requirement, we need to overload a constructor that can use the connection string as a parameter, which we will describe as follows:
Public dvapentities (String connectionString) : Base (connectionString) { }
Later we can use this constructor to get the corresponding data in the database, and here we also post the relevant code for reference.
public string Getoptionvalue (string option_name) { try { using (var db = new Ef6.dvapentities (m_ ConnectionString)) { ef6.options option = null; option = (from X in Db.options where x.optionname = = option_name && X.userid = = 0 select x). Singleordefault ()?? null; if (null! = option) { return option. OptionValue; } } Return ""; } catch (Exception ex) { return ""; } }
If you want to see the current code example click here to download!
Entity Framework 6 Custom connection string connectionstring connection MySQL