0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【保険商品管理システムの開発】顧客情報の登録処理を業務レベルに修正

0
Posted at

Models/Customer.cs

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace InsuranceProductManager.Models
{
    /// <summary>
    /// 生命保険業務用 顧客情報モデル
    /// </summary>
    public class Customer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CustomerId { get; set; }

        [Required(ErrorMessage = "氏名(漢字)は必須です。")]
        [MaxLength(100)]
        public string FullName { get; set; } = string.Empty;

        [MaxLength(100)]
        public string? FullNameKana { get; set; } // フリガナ(任意)

        [Required(ErrorMessage = "生年月日は必須です。")]
        [DataType(DataType.Date)]
        public DateTime BirthDate { get; set; }

        [Required(ErrorMessage = "性別は必須です。")]
        [MaxLength(10)]
        public string Gender { get; set; } = string.Empty; // "男性" "女性" "その他"

        [Required(ErrorMessage = "郵便番号は必須です。")]
        [RegularExpression(@"^\d{3}-\d{4}$", ErrorMessage = "郵便番号はXXX-XXXX形式で入力してください。")]
        public string PostalCode { get; set; } = string.Empty;

        [Required(ErrorMessage = "住所(都道府県・市区町村・番地)は必須です。")]
        [MaxLength(200)]
        public string Address { get; set; } = string.Empty;

        [Phone]
        [MaxLength(20)]
        public string? PhoneNumber { get; set; }

        [EmailAddress]
        public string? Email { get; set; }

        [MaxLength(100)]
        public string? Occupation { get; set; } // 職業

        [DataType(DataType.DateTime)]
        public DateTime CreatedAt { get; set; } = DateTime.Now;

        /// <summary>
        /// 年齢を算出
        /// </summary>
        public int GetAge()
        {
            var today = DateTime.Today;
            int age = today.Year - BirthDate.Year;
            if (BirthDate.Date > today.AddYears(-age)) age--;
            return age;
        }
    }
}

Views/LifeInsuranceMvc/AddCustomer.cshtml

@model InsuranceProductManager.Models.Customer

<h2>顧客登録(生命保険用)</h2>

<form asp-controller="LifeInsuranceMvc" asp-action="AddCustomer" method="post">
    <div>
        <label asp-for="FullName">氏名</label>
        <input asp-for="FullName" class="form-control" />
        <span asp-validation-for="FullName" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="FullNameKana">氏名(カナ)</label>
        <input asp-for="FullNameKana" class="form-control" />
        <span asp-validation-for="FullNameKana" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="BirthDate">生年月日</label>
        <input asp-for="BirthDate" type="date" class="form-control" />
        <span asp-validation-for="BirthDate" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="Gender">性別</label>
        <select asp-for="Gender" class="form-control">
            <option value="">選択してください</option>
            <option value="男性">男性</option>
            <option value="女性">女性</option>
            <option value="その他">その他</option>
        </select>
        <span asp-validation-for="Gender" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="PostalCode">郵便番号</label>
        <input asp-for="PostalCode" class="form-control" placeholder=": 123-4567" />
        <span asp-validation-for="PostalCode" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="Address">住所</label>
        <input asp-for="Address" class="form-control" />
        <span asp-validation-for="Address" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="PhoneNumber">電話番号</label>
        <input asp-for="PhoneNumber" class="form-control" />
        <span asp-validation-for="PhoneNumber" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="Email">ルアドレス</label>
        <input asp-for="Email" class="form-control" />
        <span asp-validation-for="Email" class="text-danger"></span>
    </div>
    <div>
        <label asp-for="Occupation">職業</label>
        <input asp-for="Occupation" class="form-control" />
        <span asp-validation-for="Occupation" class="text-danger"></span>
    </div>

    <button type="submit" class="btn btn-primary mt-2">登録</button>
</form>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

Views/LifeInsuranceMvc/CustomerList.cshtml

@model IEnumerable<InsuranceProductManager.Models.Customer>

<h2>顧客一覧</h2>

@if (Model != null && Model.Any())
{
    <table border="1" cellpadding="5">
        <thead>
            <tr>
                <th>ID</th>
                <th>氏名</th>
                <th>フリガナ</th>
                <th>生年月日</th>
                <th>年齢</th>
                <th>性別</th>
                <th>郵便番号</th>
                <th>住所</th>
                <th>電話番号</th>
                <th>Email</th>
                <th>職業</th>
                <th>登録日</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var customer in Model)
            {
                <tr>
                    <td>@customer.CustomerId</td>
                    <td>@customer.FullName</td>
                    <td>@customer.FullNameKana</td>
                    <td>@customer.BirthDate.ToString("yyyy-MM-dd")</td>
                    <td>@customer.GetAge()</td>
                    <td>@customer.Gender</td>
                    <td>@customer.PostalCode</td>
                    <td>@customer.Address</td>
                    <td>@customer.PhoneNumber</td>
                    <td>@customer.Email</td>
                    <td>@customer.Occupation</td>
                    <td>@customer.CreatedAt.ToString("yyyy-MM-dd HH:mm")</td>
                </tr>
            }
        </tbody>
    </table>
}
else
{
    <p>登録された顧客はまだいません。</p>
}

<p>
    <a asp-controller="LifeInsuranceMvc" asp-action="AddCustomer">新規顧客登録へ戻る</a>
</p>

実行時エラー

SQLite Error 1: 'table Customers has no column named Address'

つまり、
• 修正したCustomer モデルには Address プロパティがある
• でも SQLite の Customers テーブルには Address カラムが存在しない

ので、INSERT しようとしたときにコケています。

修正方法:DBを消して作り直す

rm InsuranceProductManager.db
dotnet ef migrations add Init
dotnet ef database update

UI

Adobe Express - 画面収録 2025-09-05 9.51.57.gif

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?