Problems with the entity Framework (4.3.1 version) Finishing _ Practical Tips

Source: Internet
Author: User
Here is a note of the problems that were encountered before using the entity Framework (4.3.1 version).
Update table with no primary key set
By default, the EF cannot update, insert, and delete a table that does not have a primary key. Viewing the edmx file in XML, you can see the following XML fragment in SSDL (I've defined a table Tb_withoutkey without a primary key).
Copy Code code as follows:

<entityset name= "Tb_withoutkey" entitytype= "TransferModel.Store.tb_WithoutKey" store:type= "Tables" Store:schema = "dbo" store:name= "Tb_withoutkey" >
<DefiningQuery>
SELECT
[Tb_withoutkey]. [ID] as [ID],
[Tb_withoutkey]. [Name] As [Name]
from [dbo]. [Tb_withoutkey] As [Tb_withoutkey]
</DefiningQuery>
</EntitySet>

I added a table with a primary key to compare, and in SSDL, you can see that the table with the primary key is defined as follows.
Copy Code code as follows:

<entityset name= "Tb_withkey" entitytype= "TransferModel.Store.tb_WithKey" store:type= "Tables" schema= "dbo"/>

We changed the <EntitySet> following this node without a primary key: Delete <DefiningQuery> node and change store:schema= "dbo" to Schema= "dbo". This allows us to update, delete, and insert the table that has not previously set the primary key.
Table SSDL definition with no primary key is actually more like a view, and I have a bit of an unknown store: What is the role of this namespace, why just delete <DefiningQuery> not, and also need to delete the store namespace of the schema property. These are the places I do not understand, but as a solution, it is really simple and feasible.
Change the default connection for Code-first
We know we don't even have to write a connection string when we use Code-first, but this default connection only identifies the native SQL Express version database, if you are using a different database or even a SQL Server non-Express version.
Without providing any connection to database information, EF creates a default defaultconnectionfactory, which is used by the default connection factory Sqlconnectionfactory, We can then see its constructor through reflector as follows.
Public Sqlconnectionfactory ()
{
this._baseconnectionstring = @ "Data source=.\sqlexpress; Integrated security=true; Multipleactiveresultsets=true ";
}
So the EF default can only be SQL Express version of the database. Sqlconnectionfactory provides a constructor overload, you can specify the connection string, modify the default database connection, and we can add the following nodes to the configuration file.
Copy Code code as follows:

<entityFramework>
<defaultconnectionfactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" >
<parameters>
<parameter value= "Data source=heqichang-pc; Integrated security=true; Multipleactiveresultsets=true "/>
</parameters>
</defaultConnectionFactory>
</entityFramework>

But anyway, I think it is better to specify a good connection string to develop, excluding all kinds of uncontrollable factors.
detect string truncation error
Sometimes there are errors like these when using EF.

The reason for this error is generally that the length of the field set in the database is less than the length of the new data you inserted. But know the reason, to know exactly which field length is out of range is more difficult, the EF information is not clear, of course, if the database of fewer fields, you can quickly filter out, but if the table in the field is very much, that would be troublesome. This time we can use SQL Server's own SQL Server Profiler tool (the Express version does not have this tool).
Suppose I have a tb_test table with a name field in it and a type of varchar (10). Before running our wrong program, first open Profiler, select the database you are connected to start monitoring. Finally, we can see the wrong SQL statement.

This tool makes it easy to monitor the operation of the database at the EF runtime, and note that the tool automatically recognizes that this is a request from the Entity Framework, followed by a specific SQL statement.
The above is only my last two weeks when using EF practical problems summarized, in the actual absolutely more than I encountered these problems, welcome to the park friends a lot of put forward ha!

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.