首页 技术 正文
技术 2022年11月20日
0 收藏 754 点赞 3,026 浏览 2710 个字

我在百度上搜了一下.net  core和efcore 入门案例。好多博客都是大概说了一下做法,对于小白而言还是一头雾水,我今天就抽出一点时间,写一个详细的入门小案例,就一张表没有什么业务可言。主要是操作的步骤,当然这只是让小白入个门,以后到公司工作,每个项目经理搭的架构不完全一样,但是我们懂了基本的,再做项目架构稍微复杂的就能很快上手,因为底层原理大同小异。话不多说我们开始动手做吧。

  1. 为了我们后期更好打开项目我们新建一个项目解决方案这个你们随意,咱们这个项目做

NETCOREDemo.

.Net core,EFCore 入门

2.在解决方案下,打开VS2017新建项目,选择ASP.NET  Core  Web应用程序

.Net core,EFCore 入门

3. ASP.NET Core 的版本自己可以选择,咱们这里选择2.0。选择空然后确定。

.Net core,EFCore 入门

4.添加相关引用

有两种方式

第一种采用命令行:这个我就不多说了,可以百度一下命令行安装EFCore相关包(不同的数据库包也不一样,搜索的时候关键带上自己的数据库)

第二种简单好用:我就以SQLSERVER数据库为例,我们新建好的项目有个依赖项,我们右键>点击NuGet程序包

.Net core,EFCore 入门

5.点浏览搜索一.Microsoft.EntityFrameworkCore.SqlSerVer 二. Microsoft.EntityFrameworkCore.Tools  这两个包然后安装

6.添加好引用后,继续设计数据库,采用EFCore  CodeFirst,我们先建立一个文件夹Models

在文件夹下添加这个类:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;namespace MyNoteItem.Models
{
public class Note
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] // 主键自增id
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string Title { get; set; }
[Required]
public string Content { get; set; }
public DateTime Create { get;set; }
}
}

  

7.接着在Models下创建一个NoteContext继承我们的上下文DbContext

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace MyNoteItem.Models
{
public class NoteContext:DbContext
{ public NoteContext(DbContextOptions<NoteContext> options) : base(options)
{ }
public DbSet<Note> Notes { get; set; } }
}

  

8.打开Startup.cs添加如下代码,当然连接串因自己的数据库用户和密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using MyNoteItem.Models;
using MyNoteItem.Repository;namespace MyNoteItem
{
public class Startup
{ public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
var connection = @"Server=LAPTOP-OEENOHEO\LOCAL;DataBase=Note;UID=sa;PWD=sa123;";
services.AddDbContext<NoteContext>(options=>options.UseSqlServer(connection));
services.AddScoped<INoteRepository, NoteRepository>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
} app.UseStaticFiles(); app.UseMvc(routes => //为程序注册路由,默认打开的页面
{
routes.MapRoute(
name: "default",
template: "{controller=Note}/{action=Index}/{id?}");
});
} }
}

  

9.单击VS的菜单>工具>NuGet包管理器>程序包管理控制台,打开后在程序包管理器控制台执行如下命令:

Add-Migration  NoteFirst

Update-Database

执行完出现Done表示 成功,查看数据库,看是否生成对应的数据库。如出现错误检查一下数据库连接串是否正确。EF Core 默认生成的表名为复数形式,可以在NoteContext的OnModelCreating方法改写(具体可以百度)。

10.接下来项目一步步搭建,项目结构如下:

.Net core,EFCore 入门

11.项目运行结果如下:

.Net core,EFCore 入门

12.虽然是个入门demo,代码量还是有的,所以我放在了我的GitHub上,供大家免费下载,地址如下:

https://github.com/LZYSW/.NetCoreDemo1.git

后续分页等功能。让我期待下一期的到来吧!希望对大家有用。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,906
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727