iT邦幫忙

0

.net core 抓取資料問題請教

  • 分享至 

  • twitterImage

https://ithelp.ithome.com.tw/upload/images/20210607/20110132hiT0pjfCKT.jpg

這是我的兩個TABLE

我是採用code First來建立資料庫

這是相關的屬性

Categories

 public class Categories
    {
        public Categories()
        {
            this.Products = new HashSet<Products>();
        }
        public int Id { get; set; }
        [Column(TypeName = "nvarchar(50)")]
        public string Name { get; set; }

        public virtual ICollection<Products> Products { get; set; }
    }

Products

public class Products
    {
        public Products()
        {
            this.OrderDetails = new HashSet<OrderDetails>();
        }
        [Key]
        public int Id { get; set; }
        [Column(TypeName = "nvarchar(50)")]
        public string Name { get; set; }
        [Column(TypeName = "nvarchar(MAX)")]
        public string Description { get; set; }
        public int Category_Id { get; set; }
        [Column(TypeName = "decimal(18,2)")]
        public decimal Price { get; set; }
        [Column(TypeName = "nvarchar(300)")]
        public string PhotoUrl { get; set; }
        [Column(TypeName = "nvarchar(150)")]
        public string Author { get; set; }
        [Column(TypeName = "nvarchar(50)")]
        public string Publisher { get; set; }
        public System.DateTime PublishDT { get; set; }
        public bool IsPublic { get; set; }

        public virtual Categories Categories { get; set; }
        public virtual ICollection<OrderDetails> OrderDetails { get; set; }
    }

這是我相關的查詢方法

 public IEnumerable<Products> GetAll()
        {
            var query = (from x in _context.Products
                         join y in _context.Categories
                         on x.Category_Id equals y.Id
                         select x).ToList();           
            return query;
        }

不知道為什麼
我的Categories是null
https://ithelp.ithome.com.tw/upload/images/20210607/2011013229xgy4VpUx.jpg

如果我改成這樣

 public IEnumerable<Products> GetAll()
        {
            var query = (from x in _context.Products
                         join y in _context.Categories
                         on x.Category_Id equals y.Id
                         select x).ToList();
            foreach(var dto in query)
            {
                dto.Categories = FindCategories(dto.Category_Id);
            }
            return query;
        }

        private Categories FindCategories(int category_Id)
        {
            return _context.Categories.Where(x => x.Id == category_Id).FirstOrDefault();
        }

這樣反而能抓到
https://ithelp.ithome.com.tw/upload/images/20210607/20110132xpcBS8TRR0.jpg

能否請問我錯在哪裡呢?
這是我建立的資料
https://ithelp.ithome.com.tw/upload/images/20210607/20110132LI23OzJZ8T.jpghttps://ithelp.ithome.com.tw/upload/images/20210607/20110132saD8OjWl30.jpg

更新
我好像找到原因了
我的外鍵好像是這個
https://ithelp.ithome.com.tw/upload/images/20210607/20110132K7pbFn4Iku.jpg

想請問EFCORE那裏我該如何修改呢?
我的外鍵應該是要Category_Id才對
抱歉,我很少用code first所以問題稍微多一些也蠢一些QQ

再更
這個問題暫時用DTO處理掉
日後有機會我再回頭更新這個的解決辦法

看更多先前的討論...收起先前的討論...
如果無法修改資料庫的外鍵,那你只能用非關連式的資料提取了
powerc iT邦新手 1 級 ‧ 2021-06-08 10:11:12 檢舉
https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx
不確定版本跟你適不適用
tenno081 iT邦研究生 4 級 ‧ 2021-06-08 11:10:14 檢舉
[ForeignKey(name string)]
這個方法我用過
追加後打add-migration 然後update-database嗎?
沒辦法= =
Ryan Lee iT邦新手 4 級 ‧ 2021-06-08 14:05:49 檢舉
最後一張圖關聯有錯嗎,不是 Product.CategoryId 對到 Categories.Id @@

你如果關聯有設好,你改這樣抓看看:

public IEnumerable<Products> GetAll()
{
var query = (from x in _context.Products
.Include(y => y.Categories)
select x).ToList();
return query;
}
tenno081 iT邦研究生 4 級 ‧ 2021-06-08 14:13:06 檢舉
算錯吧
我希望是 Product. Category_Id == Categories.Id
Product.CategoryId 這個好像是系統自動產生的
所以想試試看怎麼用code first的方式做修改
tenno081 iT邦研究生 4 級 ‧ 2021-06-08 14:14:08 檢舉
那段語法我有試過也是不行,整個奇葩@@
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答