VS Code+EF+PostgreSql初步搭建记录

  • 1、安装vscode、PostgreSql、.net
  • 2、修改vscode设置,指定.net路径:

  "dotnetAcquisitionExtension.existingDotnetPath": [
    

    {
      "extensionId": "ms-dotnettools.csharp",
      "path": "C:\\Program Files\\dotnet\\dotnet.exe"
    },
    {
      "extensionId": "visualstudiotoolsforunity.vstuc",
      "path": "C:\\Program Files\\dotnet\\dotnet.exe"
    }
],

3、安装以下扩展插件

4、创建并打开文件夹,打开终端并输入以下指令创建C#相关文件:

        创建解决方案sln

dotnet new sln -n myDemo

创建主程序和类库并添加到sln

dotnet new classlib -o Model
dotnet new classlib -o BLL  
dotnet new classlib -o DAL
dotnet new console -o Console

dotnet sln add .\BLL\BLL.csproj
dotnet sln add .\dal\DAL.csproj
dotnet sln add .\Model\Model.csproj
dotnet sln add .\Console\Console.csproj

项目中的各种引用:

dotnet add Console reference .\Model\Model.csproj
dotnet add Console reference .\BLL\BLL.csproj  
dotnet add Console reference .\DAL\DAL.csproj

dotnet add BLL reference .\Model\Model.csproj
dotnet add BLL reference .\DAL\DAL.csproj 

dotnet add DAL reference .\Model\Model.csproj

安装EF包,可以从终端用指令安装,也可以按F1用Nuget安装。

Nuget要梯子,但方便选择版本。

dotnet add DAL package Microsoft.EntityFrameworkCore
dotnet add DAL package Microsoft.EntityFrameworkCore.Design
dotnet add DAL package Npgsql.EntityFrameworkCore.PostgreSQL 
dotnet add DAL package System.Configuration.ConfigurationManager

模型的上下文类和实体类:

创建模型实体类,在Model下:BaseModel.cs、UserInfo.cs

上下文Context类DAL下:App.config、DaBase.cs

//App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="dbConnection"
      connectionString="PORT=5432;DATABASE=litewms;HOST=localhost;PASSWORD=a123456;USER ID=admin" />
  </connectionStrings>
</configuration>


//DbBase.cs

using Model;
using Microsoft.EntityFrameworkCore;



namespace DAL
{
    public class DbBase : DbContext
    {
        public DbSet<UserInfo> UserInfos { get; set; }
        public static string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseNpgsql(strCon);
        }


    }


}



//BaseModel.cs

using System.ComponentModel.DataAnnotations;

namespace Model
{
    [Serializable]
    public abstract class BaseModel
    {
        /// <summary>
        /// id
        /// </summary>
        [Key]
        public int id { get; set; } = 0;

    }
}


//UserInfo.cs

using System.ComponentModel.DataAnnotations.Schema;

namespace Model
{
    /// <summary>
    /// user entity
    /// </summary>
    [Table("UserInfo")]
    public class UserInfo : BaseModel
    {

        /// <summary>
        /// user's number
        /// </summary>
        #region property
        public string user_num { get; set; } = string.Empty;

        /// <summary>
        /// user's name
        /// </summary>
        public string user_name { get; set; } = string.Empty;

        /// <summary>
        /// contact
        /// </summary>
        public string contact_tel { get; set; } = string.Empty;


        #endregion

    }
}



利用模型创建数据库的UserInfo表

cd DAL
dotnet ef  migrations add UserInfo --context DbBase
dotnet ef  database update

结果如图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值