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

时间:2016-05-20 19:50:15

标签: c# sql-server entity-framework

我得到了例外,无法弄明白。

  

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

我有两张桌子。

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

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

另一张表

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

表的结构喜欢

[![12] [1] [1]

我还有对应的dbcontext

 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)
    {

    }
}

在资源库中,我有

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

在控制器中,我通过了DI。

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

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

然而,当我将鼠标悬停在_settingRespository时。我看到例外:

  

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

修改

根据评论,我修复了表名拼写错误问题。错误是SqlException: Invalid column name 'FacilitySettingOverrideSettingId',但我在stackoverflow.发现了类似的问题。也许我错误地使用了代码?

换句话说,表 FacilitySettingOverride ,它没有主键。是原因吗?

修改-1

每条评论。我重新设计了数据库。我认为Setting-FacilitySettingOverride to be 1:1 [![新] [3] [3]

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

新错误

  

SqlException:列名'FacilityId1'无效。

代码中的

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

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

我可以在上下文中获得SettingFacilitySettingOverride

1 个答案:

答案 0 :(得分:2)

Create POCO class in EF 7 code first

基本上他们是同一个问题。关键问题是创建正确的POCO类。