After completing the previous small projects, it is time to sum up experience and update study notes. Before starting the topic, let's take a look and adjust the previous learning objectives: "generate table based on Code" and "generate database script and change script" are merged into "daily use of Code First mode". This "error summary" is added ", add "Code First mode and other modes mixed use and Fluent API", "generate View" because this time is not used in the project, and then write it out after the study.
Through project practice, EF is not as easy to use as previously imagined. The problem is not caused by poor EF design. EF is actually quite convenient to use. Most databases can perform one or two lines of code and basically do not need to write SQL by themselves. There are two main problems: EF's learning resources are basically limited on the official website, because EF is relatively fast to update, and various books and materials are even less normal. In addition, EF beginners are prone to problems, various problems ...... The main purpose of this article is to share with you the problems encountered throughout the project.
The problems encountered in this project can be divided into the following categories:
1. configuration file Problems
2. Visual Studio compilation and assembly problems
3. Database Problems
4. Code problems (navigation attribute foreign key Association, Convert in linq)
At, there may be many code problems. Let's just talk about one or two of the problems I encountered.
1. configuration file Problems
The problem with the configuration file is mentioned in the second study note. Here is a simple summary.
The configuration file may involve the following problems:
1. Incorrect database connection string. Common Errors include incorrect database server name and database name spelling.
2. the user name and password in the database connection string do not have the logon permission or the password has expired.
3. The name of the database connection string configuration item is inconsistent with that of the Context.
4. EF uses the configuration file (Web. config or App. config) of the startup Item Set in VS, but the startup project does not correctly configure EF.
2. Visual Studio compilation and assembly problems
During the project development process, the svn client cleanup function was used to clear all ignored files, including the obj and Bin folders generated by compilation. The following error is reported during the next Compilation:
The System. data. sqlClient "ADO. NET provider load the Entity Framework provider type registered in the application configuration file "System. data. entity. sqlServer. sqlProviderServices, EntityFramework. sqlServer ". Make sure that the name of the restricted assembly is used and the Assembly is available to running applications. For more information, see http://go.microsoft.com/fwlink? LinkId = 260882.
After searching, it is found that the Assembly EntityFramework. SqlServer. dll is missing in the Bin folder of the WCF project. It may be because you have a problem with EF or an EF bug. I referenced this Assembly at the Db layer, and then referenced the Db project in the WCF project. Finally, the Assembly referenced by the Db was not copied to the WCF.
3. Database Problems
The database may have many problems. If the project database is not exclusive but shared, some tables may be shared. For example, this project is a tool-type project that depends on several tables of another system being developed. In fact, there will be many problems with this usage. First, this is a hybrid mode of Code First and Database First that will be introduced later. Second, the coupling between the two systems is very high. If there is a certain change to the system table of the other system, it will affect the system. At that time, in order to save the interface development workload, database access was directly adopted.
After talking about this, the first possible problem is that the database structure has changed, which is inconsistent with the entity in the Code. For example, the table name or field name has changed.
Another problem is that there is a problem with database account permissions. during development, many people may use the sa account to log on to the database. during development, there is no problem at all, but during deployment, this problem may occur. Yesterday, when the customer was deploying the system, he told me that the system could not be deployed due to a problem. After reading it remotely, he found two problems, one of which was the database account permissions. The account created by the customer has only one public permission and does not have DataReader or DataWriter. Because the customer insisted that the account used by another system can be accessed normally. I tried it myself. Without DataReader and DataWriter, the account cannot be connected to a table in SSMS. An error is reported directly for the query and the object cannot be found, with DataReader, You can query data immediately, but neither insert, update, nor delete data. After DataWriter is added, the data is normal.
During the development process, the owner of another system also deleted the _ MigrationHistory table used for Code First migration, which made it impossible to add a new migration. After this table is deleted, the system still works normally, but it will be difficult to update the database and add a new migration. I deleted all my tables and updated the database, before you can add a new migration. In this way, all the test data is lost. Fortunately, it is not important. Later, after querying the information and learning about EF6, you can customize the name of _ MigrationHistory. If you are interested, read this article on msdn.
In addition, I also heard that the database version is 2000 and EF may have problems. It is also said that 2005 may also have problems. This time it has not been verified because SQL Server 2008r2 is used during development.
4. Code Problems
When I first started using EF, I didn't have many concepts about navigation attributes. I didn't add virtual keywords to navigation attributes. I found that none of the navigation attributes were loaded during use. In the past, the delayed Loading feature was not enabled without the virtual keyword. However, you can manually use the Eagerly Loading Method to allow EF to load navigation attributes without delay. You only need to use the Include method of DbQuery <TResult>. This may be used sometimes. If you are interested, refer to this article on msdn.
Another problem is that when EF is used, it is thought that EF can be written at will like the previously used linq expression, and System is used in some conditions. if the function under Convert is run, an error is returned. Because EF needs to convert the linq expression into an SQL statement, while System. convert and other functions do not support conversion to SQL statements. Therefore, when using ef, you must note that if necessary, you can Convert it in advance.
These are the issues that you noted down for the time being, and you will find new problems later.