ef是微软推出的官方orm框架,默认防注入可以配合linq一起使用,更方便开发人员。

首先通过sqlserver现在有的数据库类生产ef

右键-》添加-》新建项,选择aod.net实体数据模型,来自数据库的code first

完成添加后会生成多个文件,并且在你的项目的配置文件中有数据库的链接字符串,下面文件中 “name=test”,

test就是连接字符串的name

public partial class testdb : dbcontext
{
    public testdb()
        : base("name=test")
    {
    }
public virtual dbset<school> school { get; set; } public virtual dbset<student> student { get; set; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { }
public partial class school
{
    [stringlength(50)]
    public string schoolid { get; set; }

    [stringlength(50)]
    public string name { get; set; }

    public datetime? createtime { get; set; }

    [stringlength(100)]
    public string address { get; set; }

    [stringlength(50)]
    public string telephone { get; set; }
}
public partial class student
{
    [stringlength(50)]
    public string studentid { get; set; }

    [stringlength(50)]
    public string name { get; set; }
}
school和student就是根据数据库表来生成的类

通过泛型来做基础操作
class basedb<t> where t : class, new()
{
    dbcontext db = new test();

    //查询
    public iqueryable<t> laodentities(system.linq.expressions.expression<func<t, bool>> wherelambda)
    {
        return db.set<t>().where<t>(wherelambda);
    }

    //更新
    public bool editentity(t entity)
    {
        db.entry<t>(entity).state = entitystate.modified;
        return db.savechanges() > 0;
    }

    //添加
    public bool addentity(t entity)
    {

        db.set<t>().add(entity);
        return db.savechanges() > 0;
    }

    //删除
    public bool deleteentity(t entity)
    {
        db.entry<t>(entity).state = entitystate.deleted;
        return db.savechanges() > 0;
    }
    //批量添加
    public bool addbatch(ilist<t> arrobj)
    {
        db.set<t>().addrange(arrobj);
        return db.savechanges() > 0;
    }
    //批量更改
    public bool updatebatch(ilist<t> arrobj)
    {
        foreach (var item in arrobj)
        {
            db.entry<t>(item).state = entitystate.modified;
        }
        return db.savechanges() > 0;
    }
    //批量删除
    public bool deletebatch(ilist<t> arrobj)
    {
        foreach (var item in arrobj)
        {
            db.entry<t>(item).state = entitystate.deleted;
        }
        return db.savechanges() > 0;
    }
    //分页
    public iqueryable<t> loadpageentities<s>(int pageindex, int pagesize, out int totalcount, system.linq.expressions.expression<func<t, bool>> wherelambda, system.linq.expressions.expression<func<t, s>> orderbylambda, bool isasc)
    {
        var temp = db.set<t>().where<t>(wherelambda);
        totalcount = temp.count();
        if (isasc)//升序
        {
            temp = temp.orderby<t, s>(orderbylambda).skip<t>((pageindex - 1) * pagesize).take<t>(pagesize);
        }
        else
        {
            temp = temp.orderbydescending<t, s>(orderbylambda).skip<t>((pageindex - 1) * pagesize).take<t>(pagesize);
        }
        return temp;
    }
}

实际使用

basedb<student> basedb = new basedb<student>();
basedb.addentity(new student {
    studentid = guid.newguid().tostring(),
    name = "小红"
});