C#C
C#3y ago
33 replies
kunio_kun

✅ Inserting record with one-to-many relationship EF Core

Hi, I'm trying to insert a record that has one-to-many relationship with other entities which leads to Foregin Key Constraint failed.

My current types look like these

using System.ComponentModel.DataAnnotations;

namespace Shared.Models;

public static class TipeCustomer
{
    public const string Personal = "Personal";
    public const string Perusahaan = "Perusahaan";
}

public class Customer
{
    public int Id { get; set; }
    [Display(Name = "Tipe")]
    public string TipeCustomer { get; set; } = Models.TipeCustomer.Perusahaan;
    [Display(Name = "Nama")]
    public string Nama { get; set; } = null!;
    [Display(Name = "Email")]
    public string? Email { get; set; }
    [Display(Name = "Nomor Telepon")]
    public string? NomorTelepon { get; set; }
    [Display(Name = "Kota")]
    public string? Kota { get; set; }
    [Display(Name = "Keterangan")]
    public string? Keterangan { get; set; }
}
Was this page helpful?