3

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.

Community
  • 1
  • 1
  • Is there an inner exception? – tofutim May 20 '16 at 19:53
  • @tofutim. No inner exception. Just a red X. The exception implied ` get { List settingList; using (SettingDbContext context = new SettingDbContext()) { settingList = context.Settings.ToList(); } return settingList; }` is the error's location –  May 20 '16 at 19:56
  • is the Settings table in the dB? – tofutim May 20 '16 at 20:01
  • Yes, it is in the DB. I just found the unhandled exception `InvalidOperationException: The navigation 'FacilitySettingOverrides' on entity type 'BandwidthRestriction.Models.Facility' has not been added to the model, or ignored, or target entityType ignored.` –  May 20 '16 at 20:03
  • Is it possible that the table declaration should be [Table("FacilitySettingOverrides")] instead of [Table("FacilitySettingOverride")]? – tofutim May 20 '16 at 20:05
  • @tofutim, No. I doubt the POCO class is wrong. Maybe need foreign key attribute but not sure. –  May 20 '16 at 20:10
  • the DB says that the Table is "FacilitySettingsOverride" as does the POCO, but the DBContext says FacilitySettingsOverrides – tofutim May 20 '16 at 20:12
  • @tofutim, if I change it, the error still there. The exception becomes `SqlException: Invalid column name 'FacilitySettingOverrideSettingId'.` –  May 20 '16 at 20:20
  • a change in exception is a good sign - why do you think it is looking for "FacilitySettingOverrideSettingId"? – tofutim May 20 '16 at 21:10
  • i think the virtual table needs to correspond to an id in the POCO – tofutim May 20 '16 at 21:11
  • @tofutim, I didn't list all code because of space. The exception is got from a httpget in controller `http://localhost:50505/api/Bandwidth/GetTotalBandwidth/1`. I reattached the image. And I think the POCO class `FacilitySettingOverride` or others might be wrong to cause the issue. –  May 20 '16 at 21:14
  • @GertArnold, as I said. Just sticking on the database schema(image). I can remove `public virtual FacilitySettingOverride FacilitySettingOverride { get; set; }` from `Setting` class. But the error is still there. Also there is another error `nvalidOperationException: The entity type 'BandwidthRestriction.Models.FacilitySettingOverride' requires a key to be defined.`. So I guess the root error is that there is no PK in the table? But I can't find the document to verify my theory. –  May 22 '16 at 21:43
  • Yes, you should always have a PK in the table. Here is a good thread on the topic: http://stackoverflow.com/questions/3996782/entity-framework-table-without-primary-key – Murray Foxcroft May 23 '16 at 09:45
  • Has been answered [here](http://stackoverflow.com/q/37398577/861716) now I guess? – Gert Arnold May 24 '16 at 06:58
  • @GertArnold, yes. You can place an answer here. –  May 24 '16 at 12:14
  • If you still need an answer here, please sanitize the question. It isn't really clear what your asking any more. – Gert Arnold May 24 '16 at 13:32

1 Answers1

2

Create POCO class in EF 7 code first

Basically they are same question. The key issue is to create the correct POCO class.

Community
  • 1
  • 1