使用EF实现登录功能

1.搭建三层,创建模型(类库.NETFormwork),数据访问(类库.NETFormwork),业务逻辑(类库.NETFormwork),表示(MVC4)2.在实体类中创建EF实体数据模型>选择来自数据库的EF设计器>下一步>选择新建连接>服务器名那一栏写上:localhost,之后在选择或输入数据库名称那栏选择要用的服务器即可。之后选择根据数据库里的实际情况来选择相...
摘要由CSDN通过智能技术生成

1.搭建三层,创建模型(类库.NETFormwork),数据访问(类库.NETFormwork),业务逻辑(类库.NETFormwork),表示(MVC4)

2.在实体类中创建EF实体数据模型>选择来自数据库的EF设计器>下一步>选择新建连接>服务器名那一栏写上:localhost,之后在选择或输入数据库名称那栏选择要用的服务器即可。之后选择根据数据库里的实际情况来选择相应的东西,表那列是必须要选择的,最后点击完成即可(如报出系统尝试修改这个提示,请点击确定)

3.上面已经完成了一个实体类的创建,值得注意的是,当数据库发生了更改时(如数据类型的变动,字段的增加)这类操作时,需要重新创建一遍实体数据与模型,所以为了不必要的麻烦,请务必设计好数据库再进行创建实体数据模型。接下来就是数据访问层中的连接数据库步骤。首先创建一个EF6.xDbConttext生成器。创建好了之后会有两个Model.Conttext.tt和Model.tt两个文件。将T4模板复制到第一个文件中即Model.Conttext.tt。复制前把其文件中的内容全部删除。T4模板如下:

<#@ template language="C#" debug="false" hostspecific="true"#>
<#@ include file="EF.Utility.CS.ttinclude"#><#@
 output extension=".cs"#>
 
<#

CodeGenerationTools code = new CodeGenerationTools(this);
MetadataLoader loader = new MetadataLoader(this);
CodeRegion region = new CodeRegion(this, 1);
MetadataTools ef = new MetadataTools(this);

string inputFile = @"..\\XuBingXue.Unified.Mode\Model.edmx";

EdmItemCollection ItemCollection = loader.CreateEdmItemCollection(inputFile);
string namespaceName = code.VsNamespaceSuggestion();

EntityFrameworkTemplateFileManager fileManager = EntityFrameworkTemplateFileManager.Create(this);

#>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XuBingXue.Unified.Mode;
 
namespace XuBingXue.Unified
{
   
<#
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
#>
	 public partial class <#=entity.Name#>Repository : BaseRepository<<#=entity.Name#>,UnifiedEntities>
     {
		
     }	
<#}#>
	
}

T4模板需要修改以下几个地方:1.string inputFile = @"..\\XuBingXue.Unified.Mode\Model.edmx"; 》2.using XuBingXue.Unified.Mode;》3.namespace XuBingXue.Unified。

第一个修改的位置是@“.\\此为要修改的地方(这里是你实体类库的名字)\Model.edmx";

第二个修改的位置是2.using XuBingXue.Unified.Mode,此为引用的路径,同样改为”你实体类库的名字“

第三个修改成你解决方案的名字即可。

3.当这些弄完之后,还要创建两个类,这两个类分别是连接数据库和对数据库的操作

下面这个是数据库操作的类的代码:

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

namespace XuBingXue.Unified.DAL
{
        public class BaseRepository<T, TS> where T : class
                                     where TS : DbContext, new()
        {
            private DbContext db = DbContextFactory<TS>.GetCurrentDbContext();


            //添加单条记录
            public bool Add(T entily)
            {
                db.Set<T>().Add(entily);
                return db.SaveChanges() > 0;

            }

            //添加多条记录
            public bool AddList(List<T> entily)
            {
                db.Set<T>().AddRange(entily);
                return db.SaveChanges() > 0;

            }

            //删除
            public bool DELETE(T entily)
            {
                db.Entry(entily).State = EntityState.Deleted;
                return db.SaveChanges() > 0;

            }

            //删除多个
            public bool BDELETE(List<T> entiles)
            {
                db.Set<T>().RemoveRange(entiles);
                return db.SaveChanges() > 0;

            }

        public virtual bool Delete(int id)
        {
            var entity = db.Set<T>().Find(id);//如果实体已经在内存中,那么就直接从内存拿,如果内存中跟踪实体没有,那么才查询数据库。
            if (entity != null) db.Set<T>().Remove(entity);
            return db.SaveChanges() > 0;
        }

        //根据id删除
        public bool BatchDELETE(params int[] entiles)
            {
                foreach (var id in entiles)
                {
                    var entity = db.Set<T>().Find(id);
                    if (entity != null)
                    {
                        db.Set<T>().Remove(entity);
                    }
                }
                return db.SaveChanges() > 0;

            }
            //修改
            public bool Update(T entily)
            {
                db.Entry(entily).State = EntityState.Modified;
                return db.SaveChanges() > 0;
            }

            //查询一个集合
            public List<T> QueryList(Expression<Func<T, bool>> 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值