What are the different Entity Framework7? The specific function

Source: Internet
Author: User

What are the different Entity Framework7? The specific function

We have introduced new features, development plans, and introductory introductions about EF7. Today, let's look at the specific new features and usage of EF7. The environment in this article is the environment introduced in the introduction to EF7.

  1. Use type conversion for columns in LINQ to Entity queries, see the following code:

1 var result = db. Blogs.where ((b = Convert.ToInt32 (b.intstring) = = 2)). FirstOrDefault ();

  Convert.ToInt32 (b.intstring) in the code to convert the column intstring to the Int32 type. Generate the SQL statement as follows:

1 SELECT TOP(1)[b].[BlogId],[b].[intstring],[b].[URL]2  from [Blog]  as [b]3 WHERE(CONVERT(int,[b].[intstring])= 2)

Note, probably a lot of friends want to use the Tostring () method in LINQ to entity, which is supported in EF6.1, usually hear many friends in exclamation, if EF7 support enumeration is good, in fact, this function is also It was supported in the EF6.1 .

 2, Code-first with Data Migration Update database using modify (ALTER) instead of DELETE (dropping) after re-create

Before this, believe that many people have suffered a loss, because before the model changes, the use of data migration to update the database structure is to delete the database, and then re-create, the problem is that it will cause data loss. I heard that some people because of unfamiliar with this function, the production environment data also lost.

3. Delete orphan (orphans) records

This, let's take a direct example to illustrate. Suppose we have a multi-relational model, blog and post, in the blog entity has the navigation property posts. Post in the database is associated with the blog through a foreign key blogid, and blogID cannot be empty. If we are in the blog entity, use the following method Posts.remove (POST); The Savechages () in the context object is then called to save the modification. It is supposed to delete the Post object's record, but the actual situation is that I got an exception. The reason for this is that the EF version is now processed: it sets the blogID of the post object removed from the blogs navigation property collective to NULL, but the corresponding foreign key in the database cannot be null, and the save fails. This post is also known as an orphan (out of relation to the parent object) object. EF7 will solve the problem. The code is as follows:

1             varresult = db. Blogs.where (b = Convert.ToInt32 (b.intstring) = =2)). FirstOrDefault ();2 3                 if(Result! =NULL)4                 {5                     varPost = db. Posts.firstordefault (p = P.postid = =1);6 result. Posts.remove (post);7 db. SaveChanges ();8}

It's an anomaly under EF6.

Unfortunately, the latest pre-release version of EF7 Ef7.0.0-beta7 has not yet solved the problem, but the content of the exception has changed.

It is believed that later versions will be processed because the EF team has committed to solving the problem.

4. Log records

Looking at the SQL generated by EF in the log, I believe this method has helped a lot of people (not all of them, because some people do not know that there is this thing, do not stop the development of learning!) )。 In EF6, you use code similar to the following to record SQL statements:

1 db. Database.Log = s = = Console.WriteLine (s);

EF7, in order to use the Microsoft.Framework.Logging log framework and Dependency injection, the interface about the log is changed over and over. The first is to remove the above Database.Log, in the EF7.0.0-BETA2 use the following way:

1               Db. Configuration.LoggerFactory.AddProvider (new  diagnosticsloggerprovider (2                 New Sourceswitch ("sourceswitch""Verbose"),  3                 new Consoletracelistener ()));    

Later, Microsoft removed the db. Configuration, using the following method:

1                 IServiceProvider contextservices = ((idbcontextservices) db). Scopedserviceprovider; 2                 var loggerfactory = contextservices.getrequiredservice<iloggerfactory>(); 3                 Oggerfactory.addconsole (Loglevel.verbose);

In the current version, it becomes the following way:

1                var service = ((iaccessor<iserviceprovider>) db). Service; 2                 var loggerfactory = service. Getrequiredservice<iloggerfactory>(); 3                 Loggerfactory.addconsole (Loglevel.verbose);

Perhaps, the later version of the BETA8,RC will be adjusted, but it can be confirmed that it will be more and more strong. From the log interface changes can also be seen, EF7 directory in the development process, the change will be very large. Therefore, the features described in the article are based on the existing version, the latter may change the changes, this point please pay attention to.

 

It's a bit uncomfortable to be here before today. Write slowly another day. Thank you for your understanding.

Entity Framework Exchange QQ Group: 458326058, welcome interested friends to join the exchange

Thank you for your continued attention, my blog address: http://www.cnblogs.com/VolcanoCloud/

  

What are the different Entity Framework7? The specific function

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.