EF迁移+应用

这篇博客介绍了如何使用Entity Framework(EF)进行数据库迁移,包括创建NewDal类进行CRUD操作,定义CodeFirst实体数据模型类Model1,BLL层的NewsBll类,以及WebAPI控制器UsersController用于展示数据。同时,提到了在WebApiConfig中配置路由,并在Web.config中设置数据库连接字符串。最后,通过命令行工具执行enable-migrations, add-migration及update-database命令来完成数据库迁移。" 117326299,8417870,深入理解Python的elif语句,"['Python编程', '控制结构', '逻辑判断']
摘要由CSDN通过智能技术生成

在这里插入图片描述

1、//NewDal类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL.EF_CodHelper
{
public class NewDal where T:class,new()
{
Model1 db = new Model1();
public List Show()
{
return db.Set().ToList();
}
public int Add(T t)
{
db.Set().Add(t);
return db.SaveChanges();
}
public int Delete(T t)
{
db.Set().Remove(t);
return db.SaveChanges();
}

    public int Update(T t)
    {
        db.Set<T>().Attach(t);
        db.Entry(t).State = System.Data.Entity.EntityState.Modified;
        return db.SaveChanges();
    }

}

}

2、//生成CodeFirst实体数据模型类Model1

namespace DAL.EF_CodHelper
{
using System;
using System.Data.Entity;
using System.Linq;
using MODEL;

public class Model1 : DbContext
{
    //您的上下文已配置为从您的应用程序的配置文件(App.config 或 Web.config)
    //使用“Model1”连接字符串。默认情况下,此连接字符串针对您的 LocalDb 实例上的
    //“DAL.EF_CodHelper.Model1”数据库。
    // 
    //如果您想要针对其他数据库和/或数据库提供程序,请在应用程序配置文件中修改“Model1”
    //连接字符串。
    public Model1()
        : base("name=Model1")
    {
    }

    //为您要在模型中包含的每种实体类型都添加 DbSet。有关配置和使用 Code First  模型
    //的详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=390109。

    // public virtual DbSet<MyEntity> MyEntities { get; set; }
        public virtual DbSet<UsersInfo> UsersInfos { get; set; }
        public virtual DbSet<CodInfo> CodInfos { get; set; }
}

//public class MyEntity
//{
//    public int Id { get; set; }
//    public string Name { get; set; }
//}

}

3、//BLL层NewsBll类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MODEL;
using DAL.EF_CodHelper;
namespace BLL
{
public class NewsBll
{
NewDal user = new NewDal();
NewDal code = new NewDal();

    public List<UsersInfo> ShowRead()
    {
        return user.Show();
    }

    public List<CodInfo> CodeShow()
    {
        return code.Show();
    }
}

}

4、//控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MODEL;
using BLL;
namespace WebApplication27.Controllers
{
public class UsersController : ApiController
{
NewsBll bll = new NewsBll();
[HttpGet]
public List ShowRead()
{
return bll.ShowRead();
}
[HttpGet]
public List CodeShow()
{
return bll.CodeShow();
}
}
}

5、//在WebApiConfig里加上/{action}/

routeTemplate: “api/{controller}/{action}/{id}”,

6、//Web.config里添加数据库连接connectionStrings

	  <add name="Model1" connectionString="Data Source=.;Initial Catalog=Month_6_11;Integrated Security=True" providerName="System.Data.SqlClient" />

在这里插入图片描述

7、//打开控制台输入命令

在这里插入图片描述
默认项目改成DAL层

在这里插入图片描述
执行命令

enable-migrations
add-migration 1
update-database

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值