简体   繁体   中英

Insert Identity using Microsoft.EntityFramework.Sqlite.Core

How would I go about modifying my Add method so it inserts a new identity each time it performs an insert. Is there a clean LINQ syntax way to do this without using hardcoded SQL?

Here is the method that saves the data to the SQLite table via Entity Framework

   public static void Add(CreditCardTableData creditCard)
        {
            using var context = new SqliteContext();
            var entity = context.CreditCards.Add(creditCard);
            entity.State = EntityState.Added;
            context.SaveChanges();
        }

Here is the class representing the table data:

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; }
    }
}

use DatabaseGenerated Attribute on the Primary key Property

Identity : ValueGeneratedOnAdd

[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CreditCardTableId { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM