Efcodefirst+t4= manipulating arbitrary databases

Source: Internet
Author: User

There have been two previous,EF select MySQL data source with EF Add the ADO entity model directly select Oracle Data source , its convenience is not much to say, use Dbfirst direct dot point can be updated with the database two-way, And the key is to facilitate our use of T4, such as:

(If you have a general understanding of T4, you will only be able to walk through the set through the ADO entity model, then you can continue to see)

But now if you're going to use a different database, but you can't find a for VS driver installation like the previous two (here's to praise MySQL with Oracle, conscience products, for me.) NET is still very friendly), then you can only use Codefirst for EF connection, but in this way, we do not only have to manually create a bunch of model, and so on, and the key of our T4 for the. edmx file is gone, this is how to complete, T4 how to traverse???

Solution, I initially felt that there are several kinds, nothing but to make a collection, the first I think is by building a. cs, each property name is consistent with the table name, to traverse the property, to get the table name collection. Unfortunately I do not get the TM Property!!!

<#@ Template debug="false"Hostspecific="true"Language="C #"#><#@ include file="EF. Utility.CS.ttinclude"#><#@ Assembly Name="e:\ Programming \c#\t4test\t4test\bin\debug\model.dll"#><#@ Output extension=". CS"#><#//var itemcollection = typeof (Assembly).            GetProperties (); //var itemcollection = typeof (Model.modeltest).                        GetProperties (); //var itemcollection = assembly.load ("T4test"). Exportedtypes.where (C=>c.fullname = = "t4test.t"). FirstOrDefault (). GetProperties ();#>

Then, using a method of file reading, a. txt file is created, and multiple table names are separated by commas.

Note: The last comma do not ignore (this is my code problem, too lazy to change)

The corresponding T4:

    

<#@ Template debug="false"Hostspecific="true"Language="C #"#><#@ include file="EF. Utility.CS.ttinclude"#><#@ Output extension=". CS"#><#                        string[] ItemCollection =New string[] {"" }; using(StreamReader reader =NewStreamReader (@"e:\ Programming \c#\t4test\t4test\txtfile.txt") //Note that the file path here is the true path to your hard drive, unless you place the TXT file in the same directory as the T4 generator.            Well understood, too?! {                stringNAMESTR =Reader.                ReadToEnd (); intindex = Namestr.lastindexof (','); NameStr= Namestr.substring (0, index); ItemCollection= Namestr.split (','); }        #>usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceGiser. TMH. idal{<#foreach(varEntityinchitemcollection) {#> Public Partial InterfaceI<#=entity#>Dal {//The public method is encapsulated in the base interface//some of the methods unique to storing Idal//to use a unique method, create a new partial class or interface for the class, do not create it here, and easily be changed T4 template after overwriting    }<#}#>}

The following is an example of PostgreSQL

1, new class library, NuGet download Npgsql driver. 2.27

  

2. New ADO entity model in class library, select Empty Codefirst

Open the configuration file, Tag 2 is the NuGet driver automatically add, tag 1 needs to be added manually, tag 3 is your database connection string, not much to say.

  

3. Open your DbContext class and create a new entity class with the corresponding Dbset attribute. Note that the red box is selected, change the table corresponding to the "namespace", without modification, the EF generated SQL table name will actually be dbo.table1, so the hint table1 does not exist, it is awkward. The friend who is familiar with PG certainly to public.table1 familiar with it, correct, change to public can, concrete see picture.

4, create a new console program or Web client, the same nuget Pgsql EF2.2.7, as above, and then the same configuration to modify, regenerate.

Test:

  

The current position, T4 and PG Codefirst All pass, like me with three layer of classmates, just need to use T4 to generate all the partial class. Everything was clean and convenient.

But here sometimes change the database, one by one T4 according to save or good trouble, there are friends have a solution please leave a message oh

Just like to use EF, even if you specify with dapper, I also change to use the EF way. 0 0. (Here are some students may want to say that EF is so rubbish, inefficient, la La ~ ~ ~ I, ignore you)

Efcodefirst+t4= manipulating arbitrary databases

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.