ASP.NET MVC コードファースト テーブルとカラム名等が異なる場合のエンティティクラス定義
Posted in ASP.NET on 6月 29th, 2011 by Site Administratorたとえば下記構造のテーブルがあったとして、
[id] int NOT NULL IDENTITY(1,1) ,
[height] int,
[taiju] int,
これに対応するエンティティクラスは
Public Class TestItem Public Property id As Integer Public Property height As Integer Public Property taiju As Integer End Class
となります。
・「taiju」ではなく「weight」としたい
・テーブルには存在しないが、bmiというパブリックな変数を追加したい
という場合には次のように属性を設定します。
Imports System.ComponentModel.DataAnnotations Public Class TestItem Public Property id As Integer Public Property height As Integer <Column("taiju")> Public Property weight As Integer <NotMapped()> Public Property bmi as Integer End Class
DBとマッピングされる項目は「R/Wで定義されたプロパティ」のみのようです。なので
Public bmi as Integer
とか
Public ReadOnly Property bmi as Integer Get Return 10000 * weight / height / height End Get End Property
という定義でもマッピング回避は可能です。