一.关于建表,以及表与表之间的关系,关系逻辑规范等等。
1.EF中 int类型做主键的特殊情况处理
EF中,主键是int类型时,会自动认为该字段是自增长,导致生成的SQL语句中不会出现这个字段。
解决办法:自增的int字段上加特性: [Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
public class dessed
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int idd { get; set; }
[StringLength(50)]
public string name { get; set; }
}
当有其他字符串做主键时,就可以有int,(相当于可以有int类型,但是必须要声明其他变量为key,否则int类型为key,就要按上面的来)
public class dessed
{
//[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int idd { get; set; }
[Key]
[StringLength(50)]
public string name { get; set; }
}
对于某个字段在数据库中是自增的,如果可以,最好也要标记出来
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] //设置自增
2.关于表之间一对一,一对多以及多对多的关系
//1.表间一对一的关系
public class StudentAddress
{
[ForeignKey("Student")]
public int StudentAddressID { get; set; }
public string Address { get; set; }
public Student Student { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public StudentAddress StudentAddress { get; set; }
}
//2.一对多的关系
public class StudentAddress
{
public int StudentAddressID { get; set; }
public string Address { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public StudentAddress StudentAddress { get; set; }
}
//3.多对多的关系
public class StudentAddress
{
public int StudentAddressID { get; set; }
public string Address { get; set; }
public ICollection<Student> Students { get; set; }
}
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
public ICollection<StudentAddress> StudentAddresses { get; set; }
}
二.关于数据库的增删改查等读取操作
1.
一.查询
1.
var allStudents = md.Students.Where(s => s.Id >= 1);
foreach (var student in allStudents)
{
Console.WriteLine(student.Id);
}
2.
var result = db.t_Department.Select(s => s).ToList();
3.
using (Model1 db = new Model1())
{
//db.Database.Log = Console.WriteLine; 检测事务开启的方式
//var query = db.de.Where(s => s.idd >= 3);
//var query = db.de.Select(i => i);
var query = db.de.Where(i => i.idd>3)
.OrderByDescending(i=>i.name) //降序,OrderBy升序,可有可无
.ToList();
string s1 = " ";
foreach (var student in query)
{
//Console.WriteLine(student.idd);
s1 = s1 + student.idd + " "+student.name;
}
MessageBox.Show(s1);
}
二.插入
1.
using (TestEntities db = new TestEntities()) //1.创建DBcontext对象
{
t_User usr = new EFtest.t_User()
{
uID = 4,
uName = "mopv,ep",
uExpireDate = DateTime.Now.AddDays(7)
};
db.t_User.Add(usr); //2.调用Add()方法插入,添加的数据必须以实体模型(比如user)的对象的形式创建
db.SaveChanges();
}
2.
db.de.Add(new dessed() { idd = 2, name = "呃呃呃" });
三.修改
1.
var stu=md.Students.Where(s => s.Id == 3).SingleOrDefault();
stu.Name = "李四";
stu.Age = 12;
md.SaveChanges();
2.
var result = db.t_User.FirstOrDefault(s => s.uID == 4);
result.uName = "哈哈哈";
db.SaveChanges();
四.删除
1.
var getStu = md.Students.Where(s => s.Id == 2).SingleOrDefault();
md.Students.Remove(getStu);
md.SaveChanges();
Console.WriteLine("删除成功!");
2.
//删除第一个
//var item = db.de.FirstOrDefault();
//db.de.Remove(item);
db.SaveChanges();
3.
var result = db.t_User.FirstOrDefault(s => s.uID == 4);
//remove()中只能指定要删除的那一条对象,当查询结果可能为多条记录时,要用FirstOrDefault()取第一条
db.t_User.Remove(result);//参数必须是一个对象,不能是一个集合
1.
EF查询表的某个字段的最大值
var showidmax=db.user.Select(s=>s.showid).Max();
2.sql语句,查询2个字段
string query = "SELECT * FROM JTGC20_2011_A5100P2_basic_properties WHERE project_id=" + project_id + " And sync_version_number=" + sync_version_number;
var all = db.Database.SqlQuery<JTGC20_2011_A5100P2_basic_properties>(query);
EF查询语句参考网址:
使用EF框架对数据库进行增删改查_TuTuTu_Tu的博客-CSDN博客
使用ADO.NET实体数据模型 - 白菜园子呀 - 博客园