简体   繁体   English

设置'引发了'System.Data.SqlClient.SqlException'类型的异常

[英]Settings' threw an exception of type 'System.Data.SqlClient.SqlException'

I got the exception and can't figure it out. 我得到了例外,无法弄明白。

Settings = '((BandwidthRestriction.Models.SettingRespository)settingRespository).Settings' threw an exception of type 'System.Data.SqlClient.SqlException' Settings ='((BandwidthRestriction.Models.SettingRespository)settingRespository).Settings'引发了类型'System.Data.SqlClient.SqlException'的异常

I have two tables. 我有两张桌子。

namespace BandwidthRestriction.Models
{
    [Table("Settings")]
    public class Setting
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string DefaultValue { get; set; }
        public string Classification { get; set; }
        public virtual FacilitySettingOverride FacilitySettingOverride { get; set; }
    }
}

And

namespace BandwidthRestriction.Models
{
    [Table("FacilitySettingOverride")]
    public class FacilitySettingOverride
    {
        [Key]
        public int FacilityId { get; set; }
        public int SettingId { get; set; }
        public string Value { get; set; }
        public virtual ICollection<Setting> Settings { get; set; }
        public virtual ICollection<Facility> Facilities { get; set; }
    }
}

Another table 另一张桌子

namespace BandwidthRestriction.Models
{
    [Table("Facilities")]
    public class Facility
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<FacilitySettingOverride> FacilitySettingOverrides { get; set; }
    }
}

The table's structure likes 桌子的结构很喜欢

[![12][1]][1] [![12] [1] [1]

Also I have the correspond dbcontext as 我也有相应的dbcontext as

 public class SettingDbContext : DbContext
{
    public DbSet<Setting> Settings { get; set; }
    public DbSet<FacilitySettingOverride> FacilitySettingOverride { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=11.53.63.94;Initial Catalog=AAA;User ID=sa;password=password;Application Name=XXX");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {

    }
}

In the respository, I have 在存储库中,我有

namespace BandwidthRestriction.Models
{
    public class SettingRespository : ISettingRespository
    {
       public List<Setting> GetAllSettings()
       {
          return Settings.ToList();
       }

       public IEnumerable<Setting> Settings
       {
           get
           {
               List<Setting> settingList;
               using (SettingDbContext context = new SettingDbContext())
               {
                   settingList = context.Settings.ToList();
               }
               return settingList;
           }
        }

In the controller, I passed the DI. 在控制器中,我通过了DI。

 [Route("api/[controller]")]
 public class BandwidthController : Controller
 {
     private readonly ISettingRespository _settingRespository;

     public BandwidthController(ISettingRespository settingRespository)
     {
         _settingRespository = settingRespository;
     }

However when I hover the _settingRespository . 但是当我将鼠标悬停在_settingRespository I see the exception: 我看到了例外情况:

Settings = '((BandwidthRestriction.Models.SettingRespository)settingRespository).Settings' threw an exception of type 'System.InvalidOperationException' Settings ='((BandwidthRestriction.Models.SettingRespository)settingRespository).Settings'引发了'System.InvalidOperationException'类型的异常

EDIT: 编辑:

Per the comment, I fixed the table name misspelling issue. 根据评论,我修复了表名拼写错误问题。 The error is SqlException: Invalid column name 'FacilitySettingOverrideSettingId' , But I found a similar question at stackoverflow. 错误是SqlException: Invalid column name 'FacilitySettingOverrideSettingId' ,但我在stackoverflow上发现了类似的问题 Maybe I used code first wrongly? 也许我错误地使用了代码?

In another word, the table FacilitySettingOverride , it doesn't have the primary key. 换句话说,表FacilitySettingOverride ,它没有主键。 Is it the cause? 是原因吗?

EDIT-1 编辑-1

Per comments. 每条评论。 I redesigned the DB. 我重新设计了数据库。 I think that Setting-FacilitySettingOverride to be 1:1 [![new][3]][3] 我认为Setting-FacilitySettingOverride to be 1:1 [![new] [3]] [3]

And

 [Table("FacilitySettingOverride")]
 public class FacilitySettingOverride
 {
    [Key]
    public int FacilityId { get; set; }

    public string Value { get; set; }
    public int SettingId { get; set; }

    public virtual Facility Facility { get; set; }
    public virtual Setting Setting { get; set; }
}

The new error is 新的错误是

SqlException: Invalid column name 'FacilityId1'. SqlException:列名称'FacilityId1'无效。

in the code: 在代码中:

    public int GetFacilityBandwidthSetting(int facilityId)
    {
        using (SettingDbContext context = new SettingDbContext())
        {
            var setting = context.Settings.Single(s => s.Name == SettingType.TotalBandwidth.ToString());
            var value = context.FacilitySettingOverride.SingleOrDefault(x => x.FacilityId == facilityId
                && x.SettingId == setting.Id);
            if (value == null)
                return int.Parse(setting.DefaultValue);
            return int.Parse(value.Value);
        }
    }

and

 [Table("Facilities")]
 public class Facility
 {
     [Key]
     public int Id { get; set; }
     public string Name { get; set; }
 }

I can getting Setting but FacilitySettingOverride in the context. 我可以在上下文中获取Setting但是FacilitySettingOverride

Create POCO class in EF 7 code first 首先在EF 7代码中创建POCO类

Basically they are same question. 基本上他们是同一个问题。 The key issue is to create the correct POCO class. 关键问题是创建正确的POCO类。

暂无
暂无

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

相关问题 发生类型为&#39;System.Data.SqlClient.SqlException&#39;的异常 - An exception of type 'System.Data.SqlClient.SqlException' occurred 类型为&#39;System.Data.SqlClient.SqlException的未处理异常 - An unhandled exception of type 'System.Data.SqlClient.SqlException 类型为“ System.Data.SqlClient.SqlException”的异常实例失败 - An exception of type 'System.Data.SqlClient.SqlException' Instance failure SqlException was Unhandled An unhandled exception of type &#39;System.Data.SqlClient.SqlException&#39;发生在 System.Data.dll - SqlException was Unhandled An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll System.Data.SqlClient.SqlException - System.Data.SqlClient.SqlException &#39;System.Data.SqlClient.SqlException&#39; - 'System.Data.SqlClient.SqlException' 关于在System.Data.dll中发生类型为&#39;System.Data.SqlClient.SqlException&#39;的未处理异常 - About the an unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll VisualStudio错误:System.Data.dll中发生了类型为&#39;System.Data.SqlClient.SqlException&#39;的未处理的异常 - VisualStudio error: An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll System.Data.dll中发生了类型为&#39;System.Data.SqlClient.SqlException&#39;的未处理的异常 - An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll System.Data.dll中出现“System.Data.SqlClient.SqlException”类型的异常 - An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM