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'
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]
Also I have the correspond 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.
[Route("api/[controller]")]
public class BandwidthController : Controller
{
private readonly ISettingRespository _settingRespository;
public BandwidthController(ISettingRespository settingRespository)
{
_settingRespository = settingRespository;
}
However when I hover the _settingRespository
. I see the exception:
Settings = '((BandwidthRestriction.Models.SettingRespository)settingRespository).Settings' threw an exception of type '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. Maybe I used code first wrongly?
In another word, the table FacilitySettingOverride, it doesn't have the primary key. Is it the cause?
EDIT-1
Per comments. I redesigned the DB. I think that 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'.
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.