随笔 - 165, 文章 - 0, 评论 - 18, 阅读 - 22万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
本文主要介绍.NET Core2.1,在EF(Entity Framework) Core中配置使用Sqlite数据库,自动创建Sqlite数据库,自动创建表。

1、安装用到的Nuget包

项目上右键 -》选择"管理Nuget程序包" -》搜索"Microsoft.EntityFrameworkCore.Sqlite" -》点击"Microsoft.EntityFrameworkCore.Sqlite"安装,还要安装"Microsoft.EntityFrameworkCore.Sqlite.Core"和"Microsoft.EntityFrameworkCore.Tools"。

Nuget使用教程

2、连接字符串和数据库中字段验证

using Microsoft.EntityFrameworkCore;
using SpiderContent.Data.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace SpiderContent.Data
{
public class SpiderContext : DbContext
{
public DbSet<PageInfo> PageInfos { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=./spider.db");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PageInfo>(entity =>
{
entity.HasKey(e => e.Id);
entity.Property(e => e.Title).IsRequired();
});
}
}
}

3、数据库初始化和工具方法代码

using Microsoft.Extensions.Configuration;
using SpiderContent.Data.Models;
using System;
using System.Linq;
using System.Reflection;
namespace SpiderContent.Data
{
    public class Utils
    {
        /// <summary>
        /// 用来初始化数据库,没有则新建数据库
        /// </summary>
        static Utils()
        {
            using (var context = new SpiderContext())
            {
                context.Database.EnsureCreated();
            }
        }
         /// <summary>
        /// 获取配置信息
        /// </summary>     
        private static IConfigurationRoot configuration;
        public static IConfigurationRoot Configuration
        {
            get
            {
                if (configuration == null)
                {
                    var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
                    configuration = builder.Build();
                }
                return configuration;
            }
        }
        public static void SaveData(PageInfo pageInfo)
        {
            using (var context = new SpiderContext())
            {
                context.PageInfos.Add(pageInfo);
                context.SaveChanges();
            }
        }
        public static void SaveOrUpdate(PageInfo pageInfo)
        {
            using (var context = new SpiderContext())
            {
                PageInfo p = context.PageInfos.Where(w => w.Url.Trim() == pageInfo.Url.Trim()).FirstOrDefault();
                if (p != null)
                {
                    p.Page = pageInfo.Page;
                    p.PlainText = pageInfo.PlainText;
                    p.Title = pageInfo.Title;
                    p.Url = pageInfo.Url;
                    p.views = pageInfo.views;
                    p.votes = pageInfo.votes;
                    p.answers = pageInfo.answers;
                    p.askedTime = pageInfo.askedTime;
                    p.askedTimeBounty = pageInfo.askedTimeBounty;
                    p.description = pageInfo.description;
                    context.Update(p);
                }
                else
                {
                    context.Add(pageInfo);
                }
                context.SaveChanges();
            }
        }
        public static PageInfo ToPageInfo(object model)
        {
            PageInfo pageInfo = new PageInfo();
            PropertyInfo property = null;
            foreach (var item in typeof(PageInfo).GetProperties())
            {
                property = model.GetType().GetProperty(item.Name);
                if (property != null)
                {
                    item.SetValue(pageInfo, property.GetValue(model));
                }
            }
            return pageInfo;
        }
    }
}
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示