PraiseJourney
PraiseJourney

Reputation: 57

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

Upvotes: 1

Views: 664

Answers (1)

Abdelrahman Gobarah
Abdelrahman Gobarah

Reputation: 1589

use DatabaseGenerated Attribute on the Primary key Property

Identity: ValueGeneratedOnAdd

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

Upvotes: 1

Related Questions