关于ABP 新增表,dbfirst模式

下面的代码是基于abp生成的项目,项目名:Store

表先在数据库里建好了,Cart这个表我用的是int自增主键

1.在Domain结尾的项目中通过EF工具生成数据实体:

Scaffold-DbContext 'Data Source=服务器IP;Initial Catalog=数据库;User Id=sa;Password=密码;Encrypt=False;' Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Tables "Cart"

生成后:public partial class Cart,改成public  class Cart : Entity<int>

如果第一次使用要在项目添加下面两个包

Microsoft.EntityFrameworkCore.Design

Microsoft.EntityFrameworkCore.SqlServer

2.实体添加到DbContext中

EF Core需要你将实体和 DbContext 建立关联.最简单的做法是在Acme.Store.EntityFrameworkCore项目的StoreDbContext类中添加DbSet属性.如下所示:

public class StoreDbContext :
    AbpDbContext<StoreDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */
    //省略了原来的代码,下面是添加的
    public DbSet<Cart> Cart { get; set; }
}
   protected override void OnModelCreating(ModelBuilder builder)
   {
        //省略了原来的代码,下面是添加的
     
       builder.Entity<Cart>(b =>
       {
           b.ToTable("Cart");
       });
}

3.Acme.Store.Application.Contracts项目添加:CartDto.cs,ICartAppService.cs

 CartDto.cs内容和上面EF工具生成实体是一样的,区别在于要继承EntityDto<int>:   public class Cart :EntityDto<int>,Cart这个表我用的是int自增主键,这个需要注意一下,EntityDto<int>这里面类型是可以根据主键类型去调整的,ICartAppService.cs内容如下

 public interface ICartAppService :
 ICrudAppService< //Defines CRUD methods
     CartDto, //Used to show 
     int, //Primary key of the  entity
     PagedAndSortedResultRequestDto>
 {

 }

4。Acme.Store.Application项目添加:CartAppService.cs

 public class CartAppService :
CrudAppService<
    Cart, 
    CartDto, 
    int, 
 PagedAndSortedResultRequestDto >, 
ICartAppService 
 {
         public CartAppService(IRepository<Cart, int> repository)
             : base(repository)
         {

         }
     }
 }

运行出现在swagger中说明成功了,

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值