Http://www.cnblogs.com/sorex/archive/2010/08/16/1800761.html I. Scenarios
If your project contains stored procedures that return multiple result sets.
If your project is to share transactions with ADO. net in the old project.
If you want to dynamically create a database table.
However, you still want to use Entity Framework. Let's continue.
Ii. Ado. NET Entity Framework extensions ()
1. Reference The efextensions. dll file.
2. AddUsingMicrosoft. Data. extensions; statement.
3. efextensions execute the T-SQL statement ?
123456789101112 |
Public Void Executesql ( String SQL) {
Using (Dbentities DB = New Dbentities ()) { Using (Db. Connection. createconnectionscope ()) { VaR cmd = dB. createstorecommand (
"Update orders set freight = 8 where orderid = 10292" , Commandtype. Text ); Cmd. executenonquery (); } } } |
Iv. efextensions
?
123456789101112 |
Public Dataset executeprocedure ()
{ Using (Dbentities DB = New Dbentities ()) { VaR cmd = dB. createstorecommand ( "Custordersorders" , Commandtype. storedprocedure, New Sqlparameter ( "Customerid"
, "Alfki" )); Sqldataadapter da = New Sqldataadapter (CMD As Sqlcommand ); Dataset DS = New Dataset ();
Da. Fill (DS ); Return DS; } } |
5. efextensions execute the T-SQL statement to obtain the entity set ?
1234567 |
Public Ienumerable <orders> getsql () {
Using (Dbentities DB = New Dbentities ())
{
Return DB. createstorecommand ( "Select * from orders" , Commandtype. Text). materialize <orders> ();
} } |
6. Allow EF to share transactions with ADO. net ?
1234567891011121314151617181920 |
Public Void Usesametran () { Using (Var tran = New Transactionscope (transactionscopeoption. Required )) { Using (Dbentities DB = New Dbentities ()) { Using (Db. Connection. createconnectionscope ()) { Orders order = dB. Orders. firstordefault (O => O. orderid = 10291 );
Order. Freight = 7; DB. savechanges (); VaR cmd = dB. createstorecommand ( "Update orders set freight = 8 where orderid = 10292" , Commandtype. Text ); Cmd. executenonquery ();
Tran. Complete (); } } } } |
7. view the T-SQL Statement of LINQ ?
123456789 |
var q = from P in context. products where p. productname. startswith ( "foo" ) select P; // before string commandtext = (objectquery ) q ). totracestring (); // After string commandtext = Q. totracestring (); |
VIII. Conclusion
Using EF for projects is also a problem that is easy to solve. Entity Framework extensions has been solved for us.
There are some other applications in the downloaded instance. You can explore the things later.
Http://www.cnblogs.com/sorex/archive/2010/08/16/1800761.htmlI. Scenarios
If your project contains stored procedures that return multiple result sets.
If your project is to share transactions with ADO. net in the old project.
If you want to dynamically create a database table.
However, you still want to use Entity Framework. Let's continue.
Ii. Ado. NET Entity Framework extensions ()
1. Reference The efextensions. dll file.
2. AddUsingMicrosoft. Data. extensions; statement.
3. efextensions execute the T-SQL statement ?
123456789101112 |
Public Void Executesql ( String SQL) { Using (Dbentities DB = New Dbentities ()) { Using (Db. Connection. createconnectionscope ()) {
VaR cmd = dB. createstorecommand ( "Update orders set freight = 8 where orderid = 10292" , Commandtype. Text ); Cmd. executenonquery (); } } } |
Iv. efextensions
?
123456789101112 |
Public Dataset executeprocedure () { Using (Dbentities DB = New Dbentities ()) { VaR cmd = dB. createstorecommand ( "Custordersorders" , Commandtype. storedprocedure, New Sqlparameter ( "Customerid" , "Alfki" )); Sqldataadapter da = New Sqldataadapter (CMD As Sqlcommand ); Dataset DS = New Dataset ();
Da. Fill (DS ); Return DS; } } |
5. efextensions execute the T-SQL statement to obtain the entity set ?
1234567 |
Public Ienumerable <orders> getsql () {
Using (Dbentities DB = New Dbentities ())
{
Return DB. createstorecommand ( "Select * from orders" , Commandtype. Text). materialize <orders> ();
} } |
6. Allow EF to share transactions with ADO. net ?
1234567891011121314151617181920 |
Public Void Usesametran () { Using (Var tran = New Transactionscope (transactionscopeoption. Required )) { Using (Dbentities DB = New Dbentities ()) { Using (Db. Connection. createconnectionscope ()) { Orders order = dB. Orders. firstordefault (O => O. orderid = 10291 );
Order. Freight = 7; DB. savechanges (); VaR cmd = dB. createstorecommand ( "Update orders set freight = 8 where orderid = 10292" , Commandtype. Text ); Cmd. executenonquery ();
Tran. Complete (); } } } } |
7. view the T-SQL Statement of LINQ ?
123456789 |
var q = from P in context. products where p. productname. startswith ( "foo" ) select P; // before string commandtext = (objectquery ) q ). totracestring (); // After string commandtext = Q. totracestring (); |
VIII. Conclusion
Using EF for projects is also a problem that is easy to solve. Entity Framework extensions has been solved for us.
There are some other applications in the downloaded instance. You can explore the things later.