使用 Microsoft.EntityFramework.Sqlite.Core 插入身份
Insert Identity using Microsoft.EntityFramework.Sqlite.Core
我将如何修改我的 Add 方法,以便它在每次执行插入时插入一个新标识。是否有一种干净的 LINQ 语法方法可以在不使用硬编码的情况下执行此操作 SQL?
这里是通过Entity Framework
将数据保存到SQL站点table的方法
public static void Add(CreditCardTableData creditCard)
{
using var context = new SqliteContext();
var entity = context.CreditCards.Add(creditCard);
entity.State = EntityState.Added;
context.SaveChanges();
}
这是代表 table 数据的 class:
using System;
using System.Collections.Generic;
using System.Text;
namespace DebtRefresh.Domain.Data.DatabaseTables
{
public class CreditCardTableData
{
public int CreditCardTableId { get; set; }
public int CreditCardType { get; set; }
public string AccountNickName { get; set; }
public float CreditLine { get; set; }
public float OwingBalance { get; set; }
}
}
在主键上使用 DatabaseGenerated Attribute 属性
Identity: ValueGeneratedOnAdd
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CreditCardTableId { get; set; }
我将如何修改我的 Add 方法,以便它在每次执行插入时插入一个新标识。是否有一种干净的 LINQ 语法方法可以在不使用硬编码的情况下执行此操作 SQL?
这里是通过Entity Framework
将数据保存到SQL站点table的方法 public static void Add(CreditCardTableData creditCard)
{
using var context = new SqliteContext();
var entity = context.CreditCards.Add(creditCard);
entity.State = EntityState.Added;
context.SaveChanges();
}
这是代表 table 数据的 class:
using System;
using System.Collections.Generic;
using System.Text;
namespace DebtRefresh.Domain.Data.DatabaseTables
{
public class CreditCardTableData
{
public int CreditCardTableId { get; set; }
public int CreditCardType { get; set; }
public string AccountNickName { get; set; }
public float CreditLine { get; set; }
public float OwingBalance { get; set; }
}
}
在主键上使用 DatabaseGenerated Attribute 属性
Identity: ValueGeneratedOnAdd
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CreditCardTableId { get; set; }