首页 > 数据库 >【WPF】EFCore 6.0生成SQLite实体对象

【WPF】EFCore 6.0生成SQLite实体对象

时间:2022-10-21 12:35:56浏览次数:48  
标签:SQLite set get 实体 entity EFCore 6.0 using public

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

环境

vs2022 + .net6.0 + 控制台+sqlite3,控制台可以生存实体;但是在Wpf中无法生存实体(不知道具体原因)。

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-PackageMicrosoft.EntityFrameworkCore.Sqlite

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

Scaffold-DbContext 'DataSource=D:\Data.db;' Microsoft.EntityFrameworkCore.Sqlite   -OutputDir Models

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

 

 

 

 

 

 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实体对象

 

标签:SQLite,set,get,实体,entity,EFCore,6.0,using,public
From: https://www.cnblogs.com/cdaniu/p/16813065.html

相关文章