[英]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.