首页 > 解决方案 > 如何在 .NET CORE 2.2 中通过代码优先方法使用 linq2db 在 Microsoft SQL Server 中创建数据库

问题描述

在 .net core 2.2 中创建了新项目并使用此包安装了 nuget 包linq2db.sqlserver我能够使用数据库优先方法进行 CRUD,但我需要知道如何使用代码优先方法进行 CRUD。

标签: c#.netsql-serverasp.net-core-2.2linq2db

解决方案


Linq2Db 有一个链接https://linq2db.github.io/index.html ,它提供了有关如何创建 POCO 并基于它创建表的更多信息。

根据页面,一个简单的 POCO 可以这样:

using System;
using LinqToDB.Mapping;

[Table(Name = "Products")]
public class Product
{
  [PrimaryKey, Identity]
  public int ProductID { get; set; }

  [Column(Name = "ProductName"), NotNull]
  public string Name { get; set; }

  // ... other columns ...
}

然后您需要配置连接字符串。


推荐阅读