1.DbSet类
解析:这个类的对象正是通过刚刚提到的Set方法获取的对象,其中的方法都与操作实体有关:
2.EF支持的完整的注释列表
解析:
[1]KeyAttribute
[2]StringLengthAttribute
[3]MaxLengthAttribute
[4]ConcurrencyCheckAttribute
[5]RequiredAttribute
[6]TimestampAttribute
[7]ComplexTypeAttribute
[8]ColumnAttribute
[9]TableAttribute
[10]InversePropertyAttribute
[11]ForeignKeyAttribute
[12]DatabaseGeneratedAttribute
[13]NotMappedAttribute
3.Fluent API
解析:由于Data Annotation不利于解耦合,所以一般使用Fluent API。Fluent API是在OnModelCreating()方法中添加代码:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
#region 定义各个表格的名称和类型
//Blog类要映射到数据库内的Blog表格,默认是blogs
var blogTable = modelBuilder.Entity<Blog>().ToTable("Blog");
var user = modelBuilder.Entity<User>();
//BlogArticle类要映射到数据库内的BlogArticle表格,默认是BlogArticles
var blogArticleTable = modelBuilder.Entity<BlogArticle>().ToTable("BlogArticle");
var blogInfo = modelBuilder.Entity<BlogInfo>().ToTable("BlogInfo");
var blogFile = modelBuilder.Entity<BlogFile>().ToTable("BlogFile");
#endregion
}
4.继承EntityTypeConfiguration<EntityType>并添加映射代码
解析:OnModelCreating方法会随着映射配置的增多而越来越大。一种更好的方法是继承EntityTyptConfiguration<EntityType>并在这个类中添加映射代码:
public class UserMap : EntityTypeConfiguration<User>
{
public UserMap()
{
this.ToTable("User");
this.HasKey(c => c.Id);
//下面是1-to-0~1关系一个user可以没有或者一个blog,对应一个或多个,是通过在实体类中写的
this.HasRequired(c => c.Blog)
.WithOptional();
}
}
然后在OnModelCreating中添加映射的配置信息:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//单独配置一条配置信息
modelBuilder.Configurations.Add(new UserMap());
//使用反射将程序集中所有的EntityTypeConfiguration<>一次性添加到modelBuilder.Configurations集合中
var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(type => !String.IsNullOrEmpty(type.Namespace))
.Where(type => type.BaseType != null && type.BaseType.IsGenericType && type.BaseType.GetGenericTypeDefinition() == typeof(EntityTypeConfiguration<>));
foreach (var type in typesToRegister)
{
dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.Configurations.Add(configurationInstance);
}
}
5.让数据类型实现IValidatableObject接口
解析:
public interface IValidatableObject
{
IEnumerable<ValidationResult> Validate(ValidationContext validationContext);
}
接口IValidatableObject定义在System.ComponentModel.DataAnnotations命名空间下。IValidatableObject接口具有唯一的方法Validate,针对自身的验证就实现在该方法中。
6.ASP.NET MVC下的四种验证编程方式
解析:
[1]手工验证绑定的参数
[2]使用ValidationAttribute特性
[3]让数据类型实现IValidatableObject接口
[4]让数据类型实现IDataErrorInfo接口
7.FluentValidation验证方式
解析:
[1]很容器创建复杂的验证规则
[2]验证规则与Model分离
[3]容器进行单元测试
8.DTO
解析:数据传输对象[DTO][Data Transfer Object],是一种设计模式之间传输数据的软件应用系统。数据传输目标往往是数据访问对象从数据库中检索数据。数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具有任何行为除了存储和检索的数据[访问和存取器]。
9.Entity Model和面向外部的Model应该分开
解析:
[1]Entity Framework Core使用的Entity Model是用来表示数据库里面的记录的。
[2]面向外部的model则表示了要传输的东西,这类model有时候叫做Dto,有时候叫做ViewModel。
10.Visual Studio和.NET Core CLI数据库迁移
解析:
[1]Visual Studio
Add-Migration 迁移名称
Update-Database
[2].NET Core CLI
dotnet ef migrations add 迁移名称
doenet ef database update
11.ASP.NET Core连接字符串存储
解析:在ASP.NET Core配置系统非常灵活,并且可以将连接字符串存储在appsettings.json、环境变量、用户密钥存储或其它配置源中。如下所示:
{
"ConnectionStrings": {
"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
},
}
上下文通常在中配置为在Startup.cs从配置中读取的连接字符串中:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}
说明:需要导入Microsoft.Extensions.Configu命名空间。
12.dotnet ef命令
解析:
13.使用SQL Server数据库
解析:
optionsBuilder.UseSqlServer("Data Source=xxx.xxx.xxx.xxx,xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx")
14.使用MySQL数据库
解析:
optionsBuilder.UseMySql("Data Source=xxx.xxx.xxx.xxx;port=xxx;Initial Catalog=xxx;user id=xxx;password=xxx;Character Set=utf8");
说明:Initial Catalog是数据库的名称。
15.Swashbuckle.AspNetCore
解析:用于生成ASP.NET Core Web API的Swagger文档。
16.NPOI
解析:这个包用来操作Excel。
17.SqlSugarCore
解析:这是一个高性能的ORM框架,操作数据库更便捷。
18.Blazor
解析:Blazor是一个基于C#、Razor模板语言和HTML的Web框架,可以通过WebAssembly在Web浏览器中运行,不需要使用javascript就可以创建动态网站。
19.Electron.NET
解析:社区受到Electron的启发,Electron.NET诞生了,可以利用.NETcore进行开发。
20.Quartz.Net
解析:Quartz.Net是纯净的,它是一个.Net程序集,是非常流行的Java作业调度系统Quartz的C#实现。
21.System.CommandLine操作
解析:
[1]允许配置命令行
[2]允许将命令行泛型参数[令牌]分析成不同的构造,其中命令行上的每个单词都是令牌
[3]调用配置为根据命令行值决定是否执行的功能
22.仓储作用
解析:用来解耦领域层与基础设施层的。
23.安装SignalR组件
解析:Microsoft.AspNetCore.SignalR
24.AspNetCoreStartupServices
解析:A diagnostic middleware nuget package for listing all services registered with Startup in an ASP.NET Core applications.
说明:https://github.com/ardalis/AspNetCoreStartupServices
25.Guid.NewGuid().ToByteArray()
解析:Returns a 16-element byte array that contains the value of this instance.