Home > Database > Mysql Tutorial > Methods and common problems using MySQL in EF_MySQL

Methods and common problems using MySQL in EF_MySQL

WBOY
Release: 2016-07-06 13:32:43
Original
1146 people have browsed it

Sometimes you need to rent space or database online. Mysql is cheaper, so you want to convert sql server to mysql...

Note: When installing Mysql, you must select the text set as utf8, otherwise you will not be able to use Chinese (currently you can also use utf8 when creating a database, but I don’t know how to set it up when EF generates a database, I hope an expert can give me some guidance)

1. Reference the EF package of mysql in the project

Install via NuGet package manager: EntityFramework6.1.3, MySql.Data.Entity6.9.8

You can also use the nuget command line to join:

Install-Package MySql.Data.Entity

2. Create new related classes

1. Create a new User entity class

And define the field length of the instance. If not defined, the error "Specified key was too long; max key length is 767 bytes" will appear. This is because the string type is longtext if it is directly mapped to mysql, and mysql supports a maximum length of 767 bytes.

public class User
{
public int Id { get; set; }
[StringLength(30)]
public string UserName { get; set; }
[MaxLength(30)]
public string PassWord { get; set; } } 
Copy after login

2. Create a new MyContext class

And explain how to use MySql to implement [DbConfigurationType(typeof(MySqlEFConfiguration))]

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MyContext : DbContext
{
public MyContext()
: base("name=MyContext")//web.config中connectionstring的名字
{
}
public DbSet<User> Users { get; set; }
}
Copy after login

3. Write test code

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyContext>());
var context = new MyContext();
//插入一行值
context.Users.Add(new User { UserName = "EF6MySQL" });
context.SaveChanges(); 
Copy after login

3. Configure Web.config

Add the following code to :

<add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=MySQL_EF;user id=root;password=root;" providerName="MySql.Data.MySqlClient" />
Copy after login

The complete web.config is as follows:

<?xml version="1.0" encoding="utf-8"?>



<add name="MyContext" connectionString="Data Source=localhost;port=3306;Initial Catalog=MySQL_EF;user id=root;password=root;" providerName="MySql.Data.MySqlClient" />
Copy after login

Finally, run the program to complete the automatic creation of the database

FAQ

•An error message appears: Specified key was too long;max key length is 767 bytes

1) Check whether the string type attribute of the entity has a length set

2) Whether the MyContext class is declared to be generated as mysql data type [DbConfigurationType(typeof(MySqlEFConfiguration))]

•An error message appears: Model compatibility cannot be checked because the database does not contain model metadata

Delete the generated database and re-run the program

•An error message appears: The sequence does not contain any matching elements

Check it out:

For example: 1.

public class Employee
{
[Key]
public int EmployeeId { get; set; }
public string Name { get; set; }
[ForeignKey("ManagerId")]
public Employee Manager { get; set; }
public int ManagerId { get; set; }
}[ForeignKey("ManagerId")] public Employee Manager { get; set; } public int ManagerId { get; set; }这个外键设置。 
Copy after login

2.[Column(TypeName="VARCHAR(254)")] public string ColumnName { get; set; } Change this definition to: [MaxLength(254)] [Column(TypeName="VARCHAR")] public string ColumnName { get; set; }3. (The following code has not been tested because I do not use it this way. It will be tested in the next article)

modelBuilder.Entity<Category>()
.HasKey(c => c.IdCategory )
.HasOptional(p => p.Children)
.WithMany()
.HasForeignKey(c => c.ChildrenId);
Copy after login

changed to:

modelBuilder.Entity<Category>()
.HasKey(c => c.IdCategory )
.HasMany(p => p.Children)
.WithOptional()
.HasForeignKey(c => c.ChildrenId);
Copy after login

.WithMany() is replaced with .WithOptional()

The above is a complete description of the methods of using MySQL in EF and common problems introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. of. Thank you very much for your support of the website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template