エンティティタイプには主キーを定義する必要があります 質問する

エンティティタイプには主キーを定義する必要があります 質問する

現在、ASP.NET Web API を作成中ですが、2 つのコントローラーではすべて正常に動作します。今、以前とまったく同じことをしようとしましたが、今度は奇妙なエラーが発生します。

System.InvalidOperationException: 「エンティティ タイプ 'UserItem' には主キーを定義する必要があります。」

では、UserItem他のものには主キーが必要ないのに、 にはなぜ主キーが必要なのでしょうか?

これは私のUserItemクラスです:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ModulApi.Models
{
    public class UserItem
    {
        public int matrikelnr { get; set; }
        public string studiengang { get; set; }
        public string user_semester { get; set; }
        public string user_max_klausur { get; set; }

        //Not necessary Constructor. I try to fix the primary Key error.
        public UserItem()
        {
            this.matrikelnr = 0;
            this.studiengang = "";
            this.user_max_klausur = "";
            this.user_semester = "";
        }
    }
}

そして私の労働者LoginItem階級は:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ModulApi.Models
{
    public class LoginItem
    {
        public long Id { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string matrikelnr { get; set; }
        public string email { get; set; }
        public string email_verified { get; set; }

        public LoginItem()
        {
            this.Id = 0;
            this.username = "";
            this.password = "";
            this.matrikelnr = "";
            this.email = "";
            this.email_verified = "";
         }
    }
}

ご覧のとおり、ゲッターとセッターは設定されているので、エラーは発生しません。

ここでエラーが発生します:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using ModulApi.Models;
using ModulApi.DBConnectors;

// For more information on enabling Web API for empty projects, visit 
https://go.microsoft.com/fwlink/?LinkID=397860

namespace ModulApi.Controllers
{
    [Route("api/user")]
    public class UserController : Controller
    {
        private readonly UserContext _context;

        public UserController(UserContext context)
        {
            _context = context;

            if (_context.UserItems.Count() == 0)  <--- Error right here
            {
                 getDataFromConnector(_context);
            }
        }

        private void getDataFromConnector(UserContext context)
        {
            //Getting my Data from Database method
        }
        .
        .

コンテキスト呼び出しなので、UserContext も添付しますが、これも LoginContext の場合と同じで、問題なく動作します。

ユーザーコンテキスト:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace ModulApi.Models
{
    public class UserContext : DbContext
    {
        public UserContext(DbContextOptions<UserContext> options) : base(options)
        {
        }

        public DbSet<UserItem> UserItems { get; set; }
    }
}

なぜこの奇妙なエラーが発生するのか、誰か分かる人はいませんか? また、まったく同じ動作をする他のすべてのコントローラーが正常に動作するのはなぜですか?

ベストアンサー1

Entity Frameworkは大会つまり、 という名前のプロパティを持つオブジェクトがある場合Id、それがオブジェクトの主キーであると想定されます。これがLoginItemクラスが正常に動作する理由です。

クラスUserItemにはそのようなプロパティがないため、主キーとして何を使用するか判断できません。

これを修正するには、キー属性クラスの主キーに何でも適用できます。例:

// Need to add the following using as well at the top of the file:
using System.ComponentModel.DataAnnotations;

public class UserItem
{
    [Key]
    public int matrikelnr { get; set; }
    public string studiengang { get; set; }
    public string user_semester { get; set; }
    public string user_max_klausur { get; set; }

    // ...
}

おすすめ記事