一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第16天,点击查看活动详情。
前言
今天我们还是来讲一讲.netcore的内容,本章讲的都是重点,主要是ORM框架以及跨域的问题,这些在大家做项目的时候也经常遇见,要是不懂的小伙伴,一起来看看吧!
EntityFrameCore6.0迁移
CodeFirst---代码先行,只关心业务,需要什么对象就先写什么对象; 必须得写有代码:
实现codefirst:
//appseting.json
"ConnectionStrings": {
"Demo": "Data Source=LAPTOP-JJ7EPCAA;Initial Catalog=EFcodefirst;Integrated Security=True;"
}
//User.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace project.Demo
{
public class User
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("CardId")]
public int CardId { get; set; }
}
}
//Card.cs
using System.ComponentModel.DataAnnotations;
namespace project.Demo
{
public class Card
{
[Key]
public int CardId { get; set; }
public string CardName { get; set; }
}
}
//CodeFirstContext.cs
using Microsoft.EntityFrameworkCore;
namespace project.Demo
{
public class CodeFirstContext : DbContext {
public CodeFirstContext()
{
}
public CodeFirstContext(DbContextOptions<CodeFirstContext> option):base(option)
{
}
public DbSet<User> User { get; set; }
public DbSet<Card> Card { get; set; }
}
}
add-migration test
update-database
Nuget引入程序集
生成迁移文件
更新到数据库中
增删改查实操
public class EFCoreCodeFirst
{
public static void Show()
{
using(CustomerDbContext context = new CustomerDbContext())
{
context.Database.EnsureDeleted(); //删除数据库
context.Database.EnsureCreated(); //创建新数据库
}
}
}
整合ASP.NETCore 6.0
整合ASP.NET Core+IOC容器
//SecendController.cs
using Iservices;
using Microsoft.AspNetCore.Mvc;
using project.GameModels;
using services;
namespace project.Controllers
{
public class SecendController : Controller
{
private readonly IAccountService _accountService;
public SecendController(IAccountService accountService)
{
_accountService = accountService;
}
public IActionResult Index()
{
//不进行依赖注入,需要传入GameDBContext
//IAccountService accountService =new AccountService(new GameDBContext());
List<Account> list = _accountService.Query<Account>(c=>true).ToList();
return View(list);
}
}
}
//BaseService.cs
using Iservices;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;
namespace services
{
public class BaseService:IBaseService
{
protected DbContext Context { get; set; }
public BaseService(DbContext context)
{
Context = context;
}
public IQueryable<T> Query<T>(Expression<Func<T,bool>>func)where T : class
{
return this.Context.Set<T>().Where(func);
}
}
}
//AccountService.cs
using Iservices;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace services
{
public class AccountService : BaseService,IAccountService
{
public AccountService(DbContext context) : base(context)
{
}
}
}
//IBaseService.cs
using System.Linq.Expressions;
namespace Iservices
{
public interface IBaseService
{
public IQueryable<T> Query<T>(Expression<Func<T, bool>> func) where T : class;
}
}
//IAccountService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Iservices
{
public interface IAccountService : IBaseService
{
}
}
视图部分**-Razor**视图
Razor混编Cshtml
局部视图
@Html.RenderPartialAsync("demo","first demo");
List列表展示
@foreach(User user in Model){
<tr>
<td>@Html.DisplayFor(modelItem=>item.Id)</td>
<td>@Html.DisplayFor(modelItem=>item.Name)</td>
</tr>
}
Razor组件
组件扩展
分页组件扩展
查询条件
增删改功能实操
数据新增
数据修改
数据删除
布局
完整页面的组合
布局
WebApi跨域问题
跨域的由来
跨域的多种解决方案
服务端允许跨域:
在方法中添加: HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
2.利用IActionFilter
public class CustomResourceAttribute : Attribute,IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
context. HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
}
public void OnActionExecuted(ActionExecutedContext context)
{
}
}
}
3.program.cs利用中间件
builder.Services.AddCors(policy =>
{
policy.AddPolicy("CorsPolicy", option => option.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithExposedHeaders());
});
app.UseCors("CorsPolicy");
ORM框架--不用关心Sql语句,只需要以类为单位,去操作数据库---以面向对象的思想来完成对数据库的操作
总结:今天讲解的内容就到这里了,大家好好理解一下,还是那句话,一步步来,加油!