Use EF6.0 to build MySQL database on VisualStadio2015

Source: Internet
Author: User
Tags connectionstrings

1. New Project

2. Create a folder for the class Dal

3. Create related classes

"Student Class"

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace ETTest3
{
public class Student
{
public int Id {get; set;}
public string LastName {get; set;}

}
}

"School Class"

Using System;
Using System.Collections.Generic;
Using System.Data.Entity.ModelConfiguration.Conventions;
Using System.Data.Entity;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Namespace ETTest3
{
public class Schoolcontext:dbcontext
{
Public Schoolcontext (): Base ("Mycontext") {}
Public dbset<student> Students {get; set;}

protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)
{
Base. Onmodelcreating (ModelBuilder);
}
}
}

4. Reference EF6.0 mysql.entity

5. Adjust the configuration File App. Config

<connectionStrings>
<add name= "mycontext" connectionstring= "Data source=localhost;port=3306;initial catalog=test;user id=root; password=123456; "Providername=" MySql.Data.MySqlClient "/>
</connectionStrings>

<entityFramework>
<defaultconnectionfactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantname= "System.Data.SqlClient" type= "System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver "/>
<provider invariantname= "MySql.Data.MySqlClient" type= "MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, version=6.9.8.0, culture=neutral, publickeytoken=c5687fc88969c44d ">
</provider></providers>
</entityFramework>

6 Test Build Database

Try
{
Schoolcontext CTX = new Schoolcontext ();
CTx. Database.initialize (TRUE);
var o = new Student ();
O.id = 1;
O.lastname = "AA";
CTx. Students.add (o);
CTx. SaveChanges ();
}
catch (Exception ex)
{
Console.Write (ex. Message);
}

7. Test Success , open the database, you can see MySQL added a library test, there is a table student

8 Processing of field changes in a data table

If the change is relatively small, such as adding or removing one or two fields
First back up the database directly after the data table operation, your data model and database table to do the corresponding on it,
It should be noted that the project on-line generally need to set DataContext
Database.setinitializer<datacontext> (NULL);
and cannot be set
Database.setinitializer<datacontext> (New dropcreatedatabaseifmodelchanges<datacontext> ());
Database.setinitializer<datacontext> (New dropcreatedatabasealways<datacontext> ()); and so on other ways,
Prevent the database from being accidentally deleted and rebuilt (because the field you added might not be the same as your model map)

If the changes are very large, you might want to switch to another scenario, migrate data, or import data yourself.

Use EF6.0 to build MySQL database on VisualStadio2015

Related Article

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.