MVC4-Code First

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   sp   on   

晚上學習MVC時遇到的問題。

第一步:添加實體類。

 /// <summary>    /// 專輯    /// </summary>    public class Album    {        public virtual int AlbumId { get; set; }        public virtual int GenerId { get; set; }        public virtual int ArtistId { get; set; }        public virtual string Title { get; set; }        public virtual decimal Price { get; set; }        public virtual string AlbumArtUrl { get; set; }        public virtual Genre Genre { get; set; }        public virtual Artist Artist { get; set; }    }    /// <summary>    /// 藝術家    /// </summary>    public class Artist    {        public virtual int ArtistId { get; set; }        public virtual string Name { get; set; }    }    /// <summary>    /// 流派    /// </summary>    public class Genre    {        public virtual int GenreId { get; set; }        public virtual string Name { get; set; }        public virtual string Description { get; set; }        public virtual List<Album> Albums { get; set; }    }

 第二步:添加控制器。

第三步:查看資料庫上下文。

 

問題來了,明明是三個實體類(外部索引鍵關聯),為什麼只產生了兩個屬性。

查看了三遍後,發現問題。Album 中的欄位GenerId 和Genre類中的GenreId不匹配。

於是乎,發現EF對於外鍵關係和資料庫名稱都有約定。而在MVC開發中,這種約定無處不在,值得注意。

 

MVC4-Code First

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.