简体   繁体   中英

Insert record into mysql db with Entity Framework

the problem is that it will insert a new record in a mysql table, I have already done the mapping of the mysql db and I have already done tests returning data and everything works. Now I read from a file, where there are queries written, I have them run me back and the result of true or false based on the final outcome of single query written to the file. Txt; I did this:

using (var w = new demotestEntities ())   
(   
    foreach (var l listaqueri)   
    (   
         var p = we.CreateQuery <category> (l);   
         we.SaveChanges ();   
         result = true;   
    )   
)

but it does not work, I sense that it returns no errors, but neither the result given written in the query. txt file is as follows:

INSERT INTO category (id, name) VALUES (null, 'test2') 

anyone can help me?

If you want to execute mysql queries through EF, this is the method I know:

var context = new demotestEntities();
context.Connection.Open();
var command = ((EntityConnection) context.Connection).StoreConnection.CreateCommand();
command.CommandText = "INSERT INTO category (id, name) VALUES (null, 'test2')";
command.ExecuteNonQuery();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM