简体   繁体   English

Entity Framework Core 5 主键问题

[英]Entity Framework Core 5 primary key issue

I have an existing SQL database with a view ServersAndServices .我有一个现有的 SQL 数据库,其中包含一个视图ServersAndServices

I have added it to my data model:我已将其添加到我的数据 model 中:

public class ServersAndServices
{
    public string ServerName { get; set; }
    public string ServiceName { get; set; }
    public string ServiceDisp { get; set; }
    public string RoleDesc { get; set; }
    public string AppName { get; set; }
    public string AppDesc { get; set; }
    public string Active { get; set; }
}

And this is my DbContext :这是我的DbContext

public class ServersAndServicesContext : DbContext
{
    public ServersAndServicesContext(DbContextOptions<ServersAndServicesContext> options)
        : base(options)
    {
    }

    public DbSet<ServersAndServices> ServersAndServices { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ServersAndServices>(eb =>
            {
                eb.HasNoKey();
                eb.ToView("ServersAndServices");
            });
    }
}

When I got to add a controller (API Controller with actions using Entity Framework Core) I get this error:当我必须添加 controller (API Controller 使用 Entity Framework Core 操作)时,我收到此错误:

Attempting to figure out the EntityFramework metadata for the model and DbContext: 'ServersAndServices'试图找出 model 和 DbContext 的 EntityFramework 元数据:'ServersAndServices'
Primary key not found.未找到主键。

I've attempted this also with the [Keyless] attribute before the class ServersAndServices but the result is the same.我也尝试过在 class ServersAndServices之前使用[Keyless]属性,但结果是一样的。

Trying to figure out what I am doing wrong here as all the documentation I have seen suggests this the correct way to handle this challenge.试图找出我在这里做错了什么,因为我看到的所有文档都表明这是处理这一挑战的正确方法。 Thank you.谢谢你。

Ivan was correct.伊万是对的。 The solution here was to just create the controller manually.这里的解决方案是手动创建 controller。 Ivan not sure how to mark a comment as the correct answer. Ivan 不确定如何将评论标记为正确答案。 Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM