【EF Core】EFCore 8.0 -CodeFirst方式生成SQLite实体对象

EFCoreFirst使用流程

1、引入工具包
Microsoft.EntityFrameworkCore.SqlServer 核心程序包,封装了关键的核心代码,使用EF必须引用这个包
Microsoft.EntityFrameworkCore.Design 设计包,用于在命令行工具下EF Core开发的工具套件
Microsoft.EntityFrameworkCore.Tools 用于数据库的生成、迁移、生成表等

 

2、数量掌握EF core 模型配置

使用Data annotations 配置模型详细请看:  https://learn.microsoft.com/zh-cn/ef/ef6/modeling/code-first/data-annotations

使用 fluent API 配置模型:https://learn.microsoft.com/zh-cn/ef/core/modeling/

尽管它们非常灵活,但请记住,DataAnnotations 仅提供你可以对 Code First 类进行的最常用的配置更改。 若要为某些边缘情况配置类,应该查看备用配置机制,即 Code First 的 Fluent API。

 

 

3.创建数据库实体

    public class Blog
    {
        public int BlogId { get; set; }
        [Required]
        [MaxLength(16)]
        public string Url { get; set; }
        public List<Post> Posts { get; } = new List<Post>();
    }

 

复制代码
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
        [Column(TypeName = "date")]
        public DateTime CreateDate { get; set; }
 
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
复制代码

2.新建一个继承DbContext的类作用是配置数据连接、操作数据库表等信息

复制代码
    public class BloggingContext: DbContext
    {
        /// <summary>
        /// Blogs表的操作属性
        /// </summary>
        public DbSet<Blog> Blogs { get; set; }
 
        /// <summary>
        /// Posts
        /// </summary>
        public DbSet<Post> Posts { get; set; }
 
        /// <summary>
        /// 配置数据连接信息
        /// </summary>
        /// <param name="optionsBuilder"></param>
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql("你的数据库连接字符串");
            base.OnConfiguring(optionsBuilder);
        }
    }
复制代码

3、 添加 App.Config 文件,App.config设置生成数据库的保存的位置(相对位置)

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings >
        <!--|DataDirectory| 表示相对目录 -->
        <add name="SqliteConnection"  connectionString="Data Source=.\Resources\Data\text.db;"     providerName="system.Data.SQLite"/>
    </connectionStrings>
</configuration>

4、wpf 窗体代码

复制代码
using IndividualQAlibrary.MVVM.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace IndividualQAlibrary.MVVM.Views
{
    /// <summary>
    /// QAlibraryView.xaml 的交互逻辑
    /// </summary>
    public partial class QAlibraryView : Window
    {
        public QAlibraryView()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            BloggingContext test = new BloggingContext();
            test.Database.EnsureCreated();
        }
    }
}
复制代码

 

 点击按钮后生成数据库

5、生成 数据库

 

posted @   小林野夫  阅读(382)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2022-03-26 【C# XML 】DTD(Document Type Definition):文档类型定义
2022-03-26 【知识图谱】 本体论
2022-03-26 【知识图谱】开篇
原文链接:https://www.cnblogs.com/cdaniu/
点击右上角即可分享
微信分享提示