首页 > 编程语言 >ASP.NET Core学习笔记3

ASP.NET Core学习笔记3

时间:2023-01-12 19:24:50浏览次数:41  
标签:Core ASP EntityFrameworkCore userInfoConfig public articleConfig NET Property Ha

ASP.NET Core学习笔记3

 

 

 

 

 

 

结论:

n Ambiguous HTTP method for action,翻译后是“不明确的HTTP操作方法”。

n 有可能是没写HTTP方法,如 [HttpGet]、[HttpPost] ,在方法上添加上即可。

n 也可能是将一些原本应该是私有的方法,写了public导致的。

 

导致错误的几种情况:

l 部分方法或者参数没有放好注释。

l 部分Public的方法没有设置好路由,仅在Controller级别设置了路由。将无需暴露的方法都设置为私有的。考虑在Controller级别设置[Route("api/[controller]/[action]")]

l 生成的XML文件没有使用相对地址。注意:默认使用的是绝对地址,源码位置一有变化就会出问题。

l XML文件需设置为“始终复制”

l 接口

数据流向:

数据库→EFContext

→IRepositories→Repositories

→IBaseServices→BaseServices

→IArticleService→ArticleService

→ArticleController→Article→ArticleDto

①→Swagger→Page/User→

②→DataBag→Razor→Page→User→

 

自定义DBContext

添加依赖包

SqlServer

1、Microsoft.EntityFrameworkCore.Design

2、Microsoft.EntityFrameworkCore.Tools

3、Microsoft.EntityFrameworkCore

4、microsoft.entityframeworkcore.sqlserver

添加依赖包

MySQL

1、Microsoft.EntityFrameworkCore.Design

2、Microsoft.EntityFrameworkCore.Tools

3、MySql.EntityFrameworkCore

4、Microsoft.EntityFrameworkCore

5、Microsoft.EntityFrameworkCore.Relational

 

创建自定义

dbContext类

public  class SwiftCodeBbsContext:DbContext

要继承DbContext

添加构造函数

  public SwiftCodeBbsContext()        {        }

        public SwiftCodeBbsContext(DbContextOptions<SwiftCodeBbsContext> options):base(options)

        {       }

创建数据集

 //创建实体对象数据集

        public DbSet<Article> Articles { get; set; }

        public DbSet<UserInfo> UserInfos { get; set; }

        public DbSet<Question> Questions { get; set; }

        public DbSet<ArticleComment> ArticleComments { get; set; }

        public DbSet<QuestionComment> QuestionComments { get; set; }

        public DbSet<Advertisement> Advertisements { get; set; }

相关设置

  protected override void OnModelCreating(ModelBuilder modelBuilder)

        {

            //文章

            //public virtual EntityTypeBuilder<TEntity> Entity<TEntity>()

            var articleConfig = modelBuilder.Entity<Article>();

            articleConfig.Property(p => p.Title).HasMaxLength(128);

           articleConfig.Property(p => p.Content).HasMaxLength(2048);

           articleConfig.Property(p => p.Tag).HasMaxLength(128);

           articleConfig.Property(p => p.CreateTime).HasColumnType("datetime");

            //Restrict 1 对于上下文跟踪的实体,删除相关主体时,依赖实体中的外键属性值将设置为 null。

            //这有助于在跟踪实体时使实体图保持一致状态,以便可以将完全一致的图形写入数据库。

            //如果属性由于不是可以为 null 的类型而无法设置为 null,则在调用 时 SaveChanges() 将引发异常。

            articleConfig.HasOne(p => p.CreateUser).WithMany().HasForeignKey(p => p.CreateUserId).OnDelete(DeleteBehavior.Restrict);

            //Cascade  3 对于由上下文跟踪的实体,在删除相关主体时,将删除依赖实体。

            articleConfig.HasMany(p => p.CollectionArticles).WithOne().HasForeignKey(p => p.ArticleId).OnDelete(DeleteBehavior.Cascade);

            articleConfig.HasMany(p => p.ArticleComments).WithOne().HasForeignKey(p => p.ArticleId).OnDelete(DeleteBehavior.Cascade);

            var articleCommentConfig = modelBuilder.Entity<ArticleComment>();

            articleCommentConfig.Property(p => p.Content).HasMaxLength(512);

            articleCommentConfig.Property(p=>p.CreateTime).HasColumnType("datetime");

            articleCommentConfig.HasOne(p => p.CreateUser).WithMany().HasForeignKey(p => p.CreateUserId).OnDelete(DeleteBehavior.Restrict);

 

 

            //用户

            var userInfoConfig = modelBuilder.Entity<UserInfo>();

            userInfoConfig.Property(p => p.UserName).HasMaxLength(64);

            userInfoConfig.Property(p => p.LoginName).HasMaxLength(64);

            userInfoConfig.Property(p => p.LoginPassWord).HasMaxLength(128);

            userInfoConfig.Property(p => p.Phone).HasMaxLength(16);

            userInfoConfig.Property(p => p.Introduction).HasMaxLength(512);

            userInfoConfig.Property(p => p.Email).HasMaxLength(64);

            userInfoConfig.Property(p => p.HeadPortrait).HasMaxLength(1024);

            userInfoConfig.Property(p => p.CreateTime).HasColumnType("datetime");

 

            //问答

            var questionConfig = modelBuilder.Entity<Question>();

            questionConfig.Property(p => p.Title).HasMaxLength(128);

            questionConfig.Property(p => p.Content).HasMaxLength(2048);

            questionConfig.Property(p => p.Tag ).HasMaxLength(128);

            questionConfig.Property(p => p.CreateTime).HasColumnType("datetime");

            questionConfig.HasOne(p => p.CreateUser).WithMany().HasForeignKey(p => p.CreateUserId).OnDelete(DeleteBehavior.Restrict);

            questionConfig.HasMany(p => p.QustionComments).WithOne(p=>p.Question).HasForeignKey(p => p.QuestionId).OnDelete(DeleteBehavior.Cascade);

 

            //问题评论

            var questionCommentConfig = modelBuilder.Entity<QuestionComment>();

            questionCommentConfig.Property(p => p.Content).HasMaxLength(512);

            questionCommentConfig.Property(p => p.CreateTime).HasColumnType("datetime");

            questionCommentConfig.HasOne(p => p.CreateUser).WithMany().HasForeignKey(p => p.CreateUserId).OnDelete(DeleteBehavior.Restrict);

 

            //广告

            var advertisementConfig = modelBuilder.Entity<Advertisement>();

            advertisementConfig.Property(p => p.ImgUrl).HasMaxLength(1024);

            advertisementConfig.Property(p => p.Url).HasMaxLength(128);

        }

连接数据库

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

        {  optionsBuilder.UseMySQL("server=localhost;database=SwiftCodeBbs;user=root;password=123456;port=3308;pooling=true;max pool size=20;persist security info=True;charset=utf8mb4;")

                //UseSqlServer(@"Server=.;Database=SwiftCodeBbs;Trusted_Connection=true;Connection Timeout=600;MultipleActiveResultSets=true;")

                .LogTo(Console.WriteLine, LogLevel.Information);

        }

 

24.定位到行首与行尾

1)home键:定位到当前行的行首;

2)end键:定位到当前行的行尾。

25.选中从光标起到行首(尾)间的代码

1)选中从光标起到行首间的代码:使用组合键“Shift + Home”;

2)选中从光标起到行尾间的代码:使用组合键“Shift + End” 

字段自动生成属性快捷键 ctrl+r,ctrl+e 有人说是加个分号 就不需要写get set了

 

标签:Core,ASP,EntityFrameworkCore,userInfoConfig,public,articleConfig,NET,Property,Ha
From: https://www.cnblogs.com/zhangdezhang/p/17047701.html

相关文章