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

 

环境

vs2022 + .net6.0 + 控制台+sqlite3,控制台可以生存实体;  如果项目(WPF 、控制台)存在App.config文件,则会导致中无法生存实体 。

DBFirst 采用Fluent API 来配置映射数据库到实体。

CodeFirst 采用在实体的属性上添加特性,将实体类映射到数据库表格中。

步骤

1、在项目下面新建Models文件夹,Models用于存放实体

 

2、新建数据库 Data

 

在数据库中新建表格

 

 

 

3 、在项目中的NuGet包管理器引入

Microsoft.EntityFrameworkCore.Tools  包含Scaffold-DbContext命令。
Microsoft.EntityFrameworkCore.Sqlite

或者“程序包管理控制台” 输入

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

4、“程序包管理控制台”  输入以下命令

Scaffold-DbContext 'DataSource=D:\Data.db;' Microsoft.EntityFrameworkCore.Sqlite   -OutputDir Models
//或
Scaffold-DbContext 'DataSource=D:\programming\SolutionQuestion\Resources\Data\IndividualQAlibraryData.db;' Microsoft.EntityFrameworkCore.Sqlite   -OutputDir MVVM/Models

5、生存成功  自动生成了DataContext 上下文、PersonInfo.cs实体、Student.cs实体

 

注意

在现存的WPF项目中输入该命令无法生存实体,不知道具体的原因,但是新建的WPF项目却可以生存实体。

 PM> Scaffold-DbContext 'DataSource=D:\Data.db;' Microsoft.EntityFrameworkCore.Sqlite   -OutputDir Modelss
Build started...

 

 

 

 

 

 DataContext 上下文 ,用于将数据库映射为实体。

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

namespace ConsoleApp1.Models
{
    public partial class DataContext : DbContext
    {
        public DataContext()
        {
        }

        public DataContext(DbContextOptions<DataContext> options)
            : base(options)
        {
        }

        public virtual DbSet<PersonInfo> PersonInfos { get; set; } = null!;
        public virtual DbSet<Student> Students { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
                optionsBuilder.UseSqlite("DataSource=D:\\Data.db;");
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PersonInfo>(entity =>
            {
                entity.HasNoKey();
                entity.ToTable("PersonInfo");
                entity.HasIndex(e => e.Email, "IX_PersonInfo_Email")
                    .IsUnique();
                entity.Property(e => e.IsCompany).HasColumnType("NUMERIC");
            });

            modelBuilder.Entity<Student>(entity =>
            {
                entity.HasKey(e => e.Name);
                entity.ToTable("student");
                entity.Property(e => e.Name).HasColumnName("name");
                entity.Property(e => e.Age).HasColumnName("age");
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}

 

PersonInfo.cs实体、

using System;
using System.Collections.Generic;

namespace ConsoleApp1.Models
{
    public partial class PersonInfo
    {
        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public string? Company { get; set; }
        public string? Email { get; set; }
        public double? TotalSales { get; set; }
        public byte[]? IsCompany { get; set; }
    }
}

 

Student.cs实体

using System;
using System.Collections.Generic;

namespace ConsoleApp1.Models
{
    public partial class Student
    {
        public string Name { get; set; } = null!;
        public long? Age { get; set; }
    }
}

 EFCore-CodeFirst方式生成SQLite实体对象

 

posted @ 2022-10-21 12:20  小林野夫  阅读(942)  评论(0编辑  收藏  举报
原文链接:https://www.cnblogs.com/cdaniu/