.NET之EntityFramework框架运用

1.创建EF模型库

创建类库--》添加新建项--》选择ADO.NET实体数据模型--》选择 来自数据库的EF选择器--》配置数据库链接以及相应的数据库--》看底部(将app.Config中链接设置另存为)默认值改成你需要空间名(DemoEntity)--》实体框架6.X--》选择相应的表--》模型命名空间;改成你需要的空间名:(Demo.EntityModel),点击完成

 

2.创建实现类库

引用刚刚的EF实体库

 

3.添加相应的NuGet包:搜索:EntityFramework

点击安装;

 

4.开始操作EF模型库

using EntityModel;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace EntityFrameworkDB
{
    class DB
    {
        public static void Add()
        {
            using (var dbContext = new DemoEntity())
            {
                var awardPool = new GXL_AwardPool()
                {
                    ShopId = 2
                };
                dbContext.GXL_AwardPool.Add(awardPool);
                dbContext.SaveChanges();
            }
        }

        public static void Update()
        {
            using (var dbContext = new DemoEntity())
            {
                var awardPool = new GXL_AwardPool()
                {
                    ID = 1,
                    ShopId = 2
                };
                dbContext.GXL_AwardPool.Attach(awardPool);
                dbContext.Entry(awardPool).State = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }

        public static void Del()
        {
            using (var dbContext = new DemoEntity())
            {
                var awardPool = new GXL_AwardPool()
                {
                    ID = 1
                };
                dbContext.GXL_AwardPool.Attach(awardPool);
                dbContext.GXL_AwardPool.Remove(awardPool);
                dbContext.SaveChanges();
            }
        }

        public static void GetModel()
        {
            using (var dbContext = new DemoEntity())
            {
                //查询单个实体
                var awardPool = dbContext.GXL_AwardPool.FirstOrDefault(x => x.ID == 1);

                //查询多个实体
                var awardPoolList = dbContext.GXL_AwardPool.Where(x => x.ShopId == 1).ToList();

                //通过sql语句查询单个实体
                //dbContext.Database.SqlQuery<ShopsMyDto>(sql).FirstOrDefault();

                //通过sql语句查谒多个实体
                //dbContext.GXL_AwardPool.Where(x => x.ShopId == 1).ToList();
            }
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public static void Tijiao()
        {
            using (var dbContext = new DemoEntity())
            {
                using (var scope = new TransactionScope())
                {
                    try
                    {
                        var awardPool = new GXL_AwardPool()
                        {
                            ShopId = 2
                        };
                        dbContext.GXL_AwardPool.Add(awardPool);

                        var awardPoolIn = new GXL_AwardPoolIn()
                        {
                            ID = 1,
                            ShopId = 2
                        };
                        dbContext.GXL_AwardPoolIn.Attach(awardPoolIn);
                        dbContext.Entry(awardPoolIn).State = EntityState.Modified;

                        //进行事务提交
                        dbContext.SaveChanges();
                        scope.Complete();
                    }
                    catch
                    {

                    }
                }
            }
        }
    }
}

 

posted @ 2017-08-19 15:32  王彬-效率开发  阅读(562)  评论(0编辑  收藏  举报