Configure some common problems and solutions for using EF _ MySQL-mysql tutorial

Source: Internet
Author: User
The prompt is not registered. The driver NoEntityFrameworkproviderfoundfortheADO. NETproviderwithinvariantname #39; System. Data. SqlClient #39;. Makesuretheproviderisregisteredi... indicates that the driver is not registered and cannot be found.

No Entity Framework provider found for the ADO. NET provider with invariant name 'system. Data. sqlclient'. Make sure the provider is registered in the 'entityframework' section of the application config file.

This problem is quite difficult. according to the above prompt, I ran to the configuration file to check whether there are entutyFramework nodes and whether to register the driver. The result is that the configuration file is pasted according to the online solution or an error is returned. then, check whether EntityFramework. SqlServer. dll has been referenced in the data access layer class library. In this case, you can search for the EntityFramework on the Internet. sqlServer. when the dll value is assigned to the local property set to True, it still does not work. after a while, I suddenly took a look at the bin \ Debug directory and found that there is only one EntityFramework. dll file, no EntityFramework. sqlServer. dll, I instantly relieved, it turned out to be like this...

Solution:

1. check whether EntityFramework. dll and EntityFramework. SqlServer. dll are introduced.

2. check whether the local copy property is set to True.

3. check whether the driver is registered in the configuration file.

For example, my SQL Server database:

4. check whether the two dll files exist in the compiled Directory. if not, copy them manually.

The system prompts that the table name is invalid.

Sometimes a new entity class is added, and a table is added to the database. Then, if you add data to the running, the prompt is displayed.

Troubleshooting process

Then there was a series of troubleshooting, and again confirmed that the table exists in the database, and manually saved a piece of data. Run the BaseContext: DbContext class and check whether there is a public DbSet. A_Emp {get; set. Run to the object class to check whether the fields match and whether the primary key [key] is marked. Finally, we compared the differences between the two entities in the previous project. Finally, I found out... Entity classes are not associated with databases

Solution

1. check whether the database table exists

2. check whether the BaseContext: DbContext class has attributes.

3. confirm fields and primary keys in the object class.

4. check whether the object is associated with the database table.

[Table ("a_Emp")]

Public class Employee

In addition, the [table] parentheses in the object class contain the database table name, and the Mark must reference the dll

Using System. ComponentModel. DataAnnotations;

Using System. ComponentModel. DataAnnotations. Schema;

When the database field record is null, the ef operation database reports an error

Troubleshooting

This problem is caused by a blank database field. an error occurs when the ef reflection assigns a value to the object, such as: ToList (),

_ Db. Set (). AddRange (entry );

_ Db. SaveChanges ();

Solution

Either of the two

1. assign values to all fields in the database. if it is null, no error is reported.

2. enter null fields in the database and use the null type in the object.

For example:

Public DataTime? CreateTime {get; set ;}

Public int? Num {get; set ;}

Public decimal? Pirce {get; set ;}

I know these two types at present, and they should be enough.

Foreign key in EF

The foreign key is a common form of data Association, which occupies an important position in data access. Next, let's take a look at several steps to use the foreign key.

1. prepare two entity classes, one primary key table and one foreign key table. for example, if the employee table requires a department number, prepare the employee class and department class.

2. add an object entity and a Department Id to Employee entity class Employee. that's right. it's not just a department ID but an entity.

[Key] public int EmpId {get; set ;}////// Name ///Public string EmpName {get; set ;}////// Gender ///Public string Sex {get; set ;}////// Birthday ///Public DateTime BirthDay {get; set ;}////// Department ///Public int DepId {get; set ;}////// Associate the primary table ///[ForeignKey ("EmpId")] public Dept {get; set;} // Note: in ForeignKey ("EmpId"), EmpId is the primary key of the employee class.

4. The Dept department table must have a primary key.

5. Call

Db. a_Emp.Include ("Dept"). Where (l => l. Dept. name. Contains (name) | string. IsNullOrEmpty (name). ToList ()

A_Emp is the entity object of the employee class, which is the public DbSet in baseContext. A_Emp {get; set ;}

You can click Dept. name directly.

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.