Summary of various issues encountered when using the Entity Framework

Source: Internet
Author: User

Here's a note of the issues you encountered before using the entity Framework (4.3.1 version).

To update a table that does not have a primary key set

By default, EF cannot perform an update, insert, or delete action on a table that does not have a primary key. To view the edmx file in XML, you can see the following XML fragment in SSDL (I defined a table Tb_withoutkey without a primary key).

<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'll add a table with a primary key to compare, as in SSDL, you can see that the table with the primary key is defined as follows.

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

We change the <EntitySet> of the non-primary key according to the above node: Delete <DefiningQuery> node, change store:schema= "dbo" to Schema= "dbo". This allows us to update, delete, and insert the table where the primary key was not previously set.

Table SSDL definition without a primary key is actually more like a view, I have a bit of an unknown store: What is the role of this namespace, why just delete <DefiningQuery> not, but also the schema property of the store namespace to delete. All of these are places I don't understand, just as a solution, it's really simple and workable.

Change the default connection for Code-first

We know we don't even have to write a connection string when using Code-first, but this default connection only recognizes the native SQL Express version of the database, if you are using a different database or even SQL Server non Express Edition.

Without providing any connection database information, EF creates a default defaultconnectionfactory, which is used by the default connection factory to Sqlconnectionfactory, We can then see its constructor by reflector as follows.

Public Sqlconnectionfactory () {    this._baseconnectionstring = @ "Data source=.\sqlexpress; Integrated security=true; Multipleactiveresultsets=true ";}

So the EF can only connect to the SQL Express version of the database by default. Sqlconnectionfactory provides a constructor overload that allows you to specify a connection string, modify the default database connection, and we can add the following nodes to the configuration file for configuration.

<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's better to specify a connection string to develop, excluding all kinds of uncontrolled factors.

Detecting string Truncation Errors

Sometimes an error such as the following occurs 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 insert. But knowing the reason, it is difficult to know which field is out of range, and the information that EF gives is not clear, of course, if there are fewer fields in the database, it can be filtered out quickly, but if there are a lot of fields in the table, it will be troublesome. At this point we can use the SQL Server Profiler that comes with SQL Server (Express version does not have this tool).

Suppose I have a tb_test table with a Name field and a type of varchar (10). Before running our wrong program, turn on Profiler and choose your connected database to start monitoring. Finally, we can see the wrong SQL statement.

This tool can easily monitor the operation of the database at the EF runtime, noting that the tool automatically distinguishes this request from the Entity Framework and then the specific SQL statement below.

The above is just my last two weeks to use EF when the actual problem summary, in practice, definitely more than the above I have encountered these problems, welcome to all the park friends put forward ha!

Summary of various issues encountered when using the Entity Framework

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.