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?

More than 5 years have passed since last update.

EntityFramework外部キー含めた複合インデックスの張り方

Last updated at Posted at 2020-04-20

メモ程度に残しておきます。

以下のようなvirtualを使って外部キーとして定義されているCustomerモデルを持つRecordモデルがある。

/// <summary>
    /// 記録
    /// </summary>
    [Description("記録")]
    [Table("Records")]
    public class Record
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public Record()
        {
        }

        // <summary>
        /// ID
        /// </summary>
        [Description("ID")]
        public long Id { get; set; }

        /// <summary>
        /// 記録日時
        /// </summary>
        [Description("記録日時")]
        public DateTime RecordAt { get; set; }

        /// <summary>
        /// 顧客
        /// </summary>
        [Required]
        public virtual Customer Customer { get; set; }
    }

    /// <summary>
    /// 顧客
    /// </summary>
    [Description("顧客")]
    [Table("Customers")]
    public class Customer
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public Customer()
        {
        }

        // <summary>
        /// ID
        /// </summary>
        [Description("ID")]
        public long Id { get; set; }

        /// <summary>
        /// 名前(姓)
        /// </summary>
        [Description("名前(姓)")]
        [Required]
        [StringLength(20)]
        public string LastName { get; set; }

        /// <summary>
        /// 名前(名)
        /// </summary>
        [Description("名前(名)")]
        [Required]
        [StringLength(20)]
        public string FirstName { get; set; }
    }

このモデルにRecordAtとCustomer.Idの2つで複合indexを張りたいパターン。

/// <summary>
    /// 記録
    /// </summary>
    [Description("記録")]
    [Table("Records")]
    public class Record
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public Record()
        {
        }

        // <summary>
        /// ID
        /// </summary>
        [Description("ID")]
        public long Id { get; set; }

        /// <summary>
        /// 記録日時
        /// </summary>
        [Description("記録日時")]
        [Index("IX_Customer_Id_RecordAt", 2)]
        public DateTime RecordAt { get; set; }

        /// <summary>
        /// CustomerプロパティのIdの値が入る
        /// </summary>
        [ForeignKey("Customer")]
        [Index("IX_Customer_Id_RecordAt", 1)]
        public long Customer_Id { get; set; }

        /// <summary>
        /// 顧客
        /// </summary>
        [Required]
        public virtual Customer Customer { get; set; }
    }

    /// <summary>
    /// 顧客
    /// </summary>
    [Description("顧客")]
    [Table("Customers")]
    public partial class Customer
    {
        /// <summary>
        /// コンストラクタ
        /// </summary>
        public Customer()
        {
        }

        // <summary>
        /// ID
        /// </summary>
        [Description("ID")]
        public long Id { get; set; }

        /// <summary>
        /// 名前(姓)
        /// </summary>
        [Description("名前(姓)")]
        [Required]
        [StringLength(20)]
        public string LastName { get; set; }

        /// <summary>
        /// 名前(名)
        /// </summary>
        [Description("名前(名)")]
        [Required]
        [StringLength(20)]
        public string FirstName { get; set; }
    }

ForeignKey属性を付けると「外部キー制約のモデル名_プロパティ名」でCustomer.Idの値がセットされるプロパティを定義できる。
Index属性は(キー名, Order)で複合インデックスを張ることができる。

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?