1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【保険商品管理システムの開発】Product.csの解説

Posted at

役割

保険商品などのデータ構造(モデル)**を定義しており、ASP.NET Core + Entity Framework Core 環境で データベースのテーブルの構造を決定する役割を担います。

コード

Models/Product.cs
// Models/Product.cs

📁 Product.cs は Models フォルダにあることを示します。
→ アプリ内で 「データモデル」=データの設計図として使います。

Models/Product.cs
using System.ComponentModel.DataAnnotations;

✅ DataAnnotations を使うことで、プロパティに対してバリデーション(例: 必須、最大長など)を設定できます。

Models/Product.cs
namespace InsuranceProductManager.Models

✅ 名前空間(namespace)はこのクラスが属するグループを表しています。
他のファイルから Product を参照するときにこの名前空間が必要です。

Models/Product.cs
public class Product

✅ Product クラスを定義しています。
このクラスはデータベースの Products テーブルとマッピングされることになります。

Models/Product.cs
public int Id { get; set; }

🔑 主キー(Primary Key)。Entity Framework は Id という名前を自動で識別して主キーにします。

Models/Product.cs
[Required]
public string Name { get; set; }

✅ Name プロパティは必須(null 不可)です。[Required] により、DBにも NOT NULL 制約がかかります。

Models/Product.cs
public string Description { get; set; }

📝 商品の説明です。任意(null 可能)で、バリデーションは付けていません。

Models/Product.cs
public decimal Price { get; set; }

💰 商品の価格です。decimal 型は通貨・金額計算に適したデータ型です。

Models/Product.cs
public bool IsPrivate { get; set; }

🔒 この商品が**「非公開(private)」かどうか**を表す bool 型プロパティです。
true なら非公開、false なら公開という意味になります。

Models/Product.cs
var privateProducts = _context.Products.Where(p => p.IsPrivate);

このプロパティを利用すれば、

var privateProducts = _context.Products.Where(p => p.IsPrivate);

のようにして「非公開の商品だけ」を取得できます。

まとめ

プロパティ名 用途
Id 主キー(商品ID)
Name 商品名(必須)
Description 説明文(任意)
Price 金額(decimal型)
IsPrivate 非公開フラグ(true=非公開)
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?