如果抛出错误,请从数据库中选择实体

问题描述 投票:0回答:1

我将 MySQL 数据库迁移到 SQL Server。我的项目使用实体框架。我有数据库课

public class Database : DbContext
{
    public Database() { }

    public Database(DbContextOptions<Database> options) : base(options)
    {
    }

    public DbSet<AccessGroup> AccessGroups { get; set; }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Server=my_server;User ID=user;password=password;database=suz;Initial Catalog=suz;Trusted_Connection=False;TrustServerCertificate=True;Encrypt=False;MultipleActiveResultSets=true;");
    }
}

和AccessGroup类

[Table("accessgroups")]
public class AccessGroup
{
    public long Id { get; set; }
    public string Name { get; set; }

    public bool CanSubscribe { get; set; }
    public bool CanSeeHisTickets { get; set; }
    public bool CanSeeCompanyTickets { get; set; }
    public bool CanSeeAllTickets { get; set; }
    public bool CanEditTickets { get; set; }
    public bool CanDeleteTickets { get; set; }
    public bool CanSeeServiceComments { get; set; }
    public bool CanRegisterUsers { get; set; }
    public bool CanSelectTopic { get; set; }
    public bool CanEditUsers { get; set; }
    public bool CanSelectUrgency { get; set; }
    public bool CanTakeTickets { get; set; }
    public bool CanAssignTickets { get; set; }
    public bool CanFinishTickets { get; set; }
    public bool CanMoveTickets { get; set; }
}

但是当我尝试从数据库中获取它时,它会抛出错误:

database.AccessGroups.ToList()

Microsoft.Data.SqlClient.SqlException(0x80131904):无效的对象名称“accessgroups”。

这是它执行的查询

 SELECT [a].[Id], [a].[CanAssignTickets], [a].[CanDeleteTickets], [a].[CanEditTickets], [a].[CanEditUsers], [a].[CanFinishTickets], [a].[CanMoveTickets], [a].[CanRegisterUsers], [a].[CanSeeAllTickets], [a].[CanSeeCompanyTickets], [a].[CanSeeHisTickets], [a].[CanSeeServiceComments], [a].[CanSelectTopic], [a].[CanSelectUrgency], [a].[CanSubscribe], [a].[CanTakeTickets], [a].[Name]
  FROM [accessgroups] AS [a]

there is my database configuration

我该如何解决这个问题?

c# sql-server entity-framework
1个回答
1
投票

该表在

suz
模式中定义。您需要在
Table
属性或
OnModelCreating
中指定,例如:

[Table("accessgroups", Schema="suz")]
public class AccessGroup
© www.soinside.com 2019 - 2024. All rights reserved.