??EntityFramework6?????SQL???
???????????? ???????[ 2016/5/6 10:51:09 ] ????????????? SQL
??????????????????????????EF6??????????CRDU??????????????д???SQL????????в?????????????????????????????EF6?????SQL???
?????????????????EF????????дSQL??????дSQL??????????ADO.NET??????????????????????Щ??????EF??????????????Щ?????????????????????????????????????????????????????????????Щ????????????????????????????????????????鷳???????????????????????SQL????????????????
?????????EF???SQL???ADO.NET?????????????в?????????EF???????????????????浽????????У????????DataReader???鷳??????????????????????и?????????????????????????????????????.SaveChanges()?????μ?????????
????ExecuteSqlCommand??SqlQuery
????????????????DBModel????????и?Database??????????????鷽??.ExecuteSqlCommand()??.SqlQuery()??????????????SQL????????.ExecuteSqlCommand()?????????????????????????????????.ExecuteSqlCommand()???????д???????????????????.SqlQuery()????????????????????????????????????У???????????в????????
???????.ExecuteSqlCommand()????????????????
????.ExecuteSqlCommand()????÷???????????????SQL???????????????????????????????
using (var db = new DBModel()) //???????????????
{
//??????????SQL??????????????????
int result = db.Database.ExecuteSqlCommand(@"CREATE TABLE `test`.`test` (
`id` INT NOT NULL??
PRIMARY KEY(`id`)); ");
//???SqlParameter??????????SQL???
var p_name = new SqlParameter("@name"?? "????");
var p_age = new SqlParameter("@age"?? 13);
//?????????MySql????? ???SqlParameter???滻?MySqlParameter
//var p_name = new MySqlParameter("@name"?? "????");
//var p_age = new MySqlParameter("@age"?? 13);
//???????????
result = db.Database.ExecuteSqlCommand(@"UPDATE `test`.`student`
SET `age` = @age
WHERE `name` = @name;"?? p_age?? p_name);
//?????????SQL??????????????????
Task<int> result2 = db.Database.ExecuteSqlCommandAsync("DROP TABLE `test`.`test`;");
}
????PS?????????????????????????Database?????л?????.Create() ??.Delete()???????????????????????????bool??????г????????
???????.SqlQuery()???????
????????????????.SqlQuery()????????в?????.SqlQuery()??????????????????????????????????????????????????????????????student??????????????ж??????????????????????????????int??
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????а??????string?????name??int?????age?????????????????
class temp
{
public string name { get; set; }
public int age { get; set; }
}
static void Main(string[] args)
{
using (var db = new DBModel()) //???????????????
{
//?????????????????????????????????student
DbRawSqlQuery<student> result1 = db.Database.SqlQuery<student>("SELECT * FROM test.student WHERE name = '????'");
//?????????????????????
//DbRawSqlQuery result1 = db.Database.SqlQuery(typeof(student)?? "SELECT * FROM test.student WHERE name = '????'");
Console.WriteLine(result1.FirstOrDefault().name); //???????
DbRawSqlQuery<int> result2 = db.Database.SqlQuery<int>("SELECT count(*) FROM test.student");
Console.WriteLine(result2.FirstOrDefault()); //????ж??????
//???????????????????
var result3 = db.Database.SqlQuery<temp>("SELECT `name`??`age` FROM test.student;");
foreach (temp item in result3)
{
Console.WriteLine(item.name + ":" + item.age);
}
}
???????DbSet<T>?μ?.SqlQuery()
???????????????彎??DbSet<T>????????.SqlQuery()??????????????????????????DbSet<T>?μ?.SqlQuery()??????DbSet<T>?а????????????DbSet<T>?μ?.SqlQuery()??????????????????????????????DBModel??????????????????????????????????????????????.SaveChanges()???????????????????.Database.SqlQuery()????????????????????
using (var db = new DBModel()) //???????????????
{
//???????????????????????????????
student result1 = db.students.SqlQuery("SELECT * FROM test.student WHERE name = '????'").FirstOrDefault();
result1.age = 13; //?????彎????.SqlQuery?????????????????????????浽??????
student result2 = db.Database.SqlQuery<student>("SELECT * FROM test.student WHERE name = '????'").FirstOrDefault();
result2.age = 21; //?????????.Database.SqlQuery????????????????????????浽?????
//??????.Database.SqlQuery?2???????????????????浽?????
student result3 = db.Database.SqlQuery<student>("SELECT * FROM test.student WHERE name = 'С??'").FirstOrDefault();
result3.age = 36;
db.Entry<student>(result3).State = System.Data.Entity.EntityState.Modified; //????????????????????????????
db.SaveChanges();
}
????????????EF6?е???ò????????????????????????????????ó?????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11