c# - 实体类型需要定义主键

标签 c# asp.net asp.net-web-api

我现在正在编写一个 ASP.NET Web API,对于 2 个 Controller ,一切都运行良好。现在我尝试做与以前完全相同的事情,但这次我得到一个奇怪的错误:

System.InvalidOperationException: "The entity type 'UserItem' requires a primary key to be defined."

那么,为什么 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 = "";
         }
    }
}

如您所见,我设置了 getter 和 setter,所以不会出现错误。

这里是错误发生的地方:

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
        }
        .
        .

好吧,因为它在 Context 调用中,我也会附加 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; }
    }
}

有谁知道为什么我会收到这个奇怪的错误?为什么所有其他 Controller 都能正常工作,它们的功能完全相同?

最佳答案

Entity Framework 通过 convention 进行.这意味着如果您有一个对象具有名为 Id 的属性,它将假定它是该对象的主键。这就是为什么您的 LoginItem 类可以正常工作的原因。

您的 UserItem 类没有这样的属性,因此它无法确定将什么用作主键。

要解决此问题,请添加 KeyAttribute无论你的主键在你的课上是什么。例如:

// 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; }

    // ...
}

关于c# - 实体类型需要定义主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48225989/

相关文章:

c# - 使用反射初始化对象的自动属性——有什么注意事项吗?

c# - BackgroundWorker 和 WebBrowser 控件

css - ASP 文本框 CSS 和数据绑定(bind)表达式问题

c# - 依赖注入(inject)以解决与运行时数据的依赖关系

c# - CIL - 装箱/拆箱与可空

c# - 如何在 json 序列化中跳过属性名称?

ASP.Net:尝试处理 http://localhost:5957/http://yahoo.com 时出现 HTTP 400 错误请求错误

asp.net - 什么是 ASP.NET 中的跨页面发布?

javascript - 管理 signalR 通知以同步客户端和服务器 (c#)

c# - 在Web API中保存图像时无法解析 "A generic error occurred in GDI+"