C# 名为Nb_delays_90+的列属性出错_曾经(有特殊字符)
我有一个带字段的代码第一个表类:C# 名为Nb_delays_90+的列属性出错_曾经(有特殊字符),c#,sql-server,entity-framework,ef-code-first,C#,Sql Server,Entity Framework,Ef Code First,我有一个带字段的代码第一个表类: public class NBKI_Response { [Column("Nb_delays_90+_ever")] public int? Nb_delays_90__ever { get; set; } [Column("Nb_delays_90+_2y")] public int? Nb_delays_90__2y { get; set; } } 尝试添加新记录时出现错误“System.Data.Sql
public class NBKI_Response
{
[Column("Nb_delays_90+_ever")]
public int? Nb_delays_90__ever { get; set; }
[Column("Nb_delays_90+_2y")]
public int? Nb_delays_90__2y { get; set; }
}
尝试添加新记录时出现错误“System.Data.SqlClient.SqlException:无效列名”
为什么以及如何修复
在数据库中,列的名称正好是“Nb_延迟_90+_曾经”
UPD:不允许我更改数据库中的列名
UPD2:表的脚本(我首先使用现有数据库中的代码):
UPD3添加跟踪后,通过:
db.Database.Log = m => Console.WriteLine(m);
我发现按列属性映射不起作用:
INSERT [dbo].[NBKI_Response]([External_Data_Id], [Total_accounts],
[Total_negative_accounts], [Total_active_accounts], [Total_credit_limit],
[Total_overdue_amount], [Total_outstanding_amount],
[Total_installment_amount], [Total_disputed], [Total_letigation],
[Total_bankruptcy], [Total_inquiries], [Recent_inquiries],
[Recent_account_date], [Oldest_account_date], [Nb_delays_5_30_ever],
[Nb_delays_30_60_ever], [Nb_delays_60_90_ever], [Nb_delays_90__ever],
[Nb_delays_5_30_2y], [Nb_delays_30_60_2y], [Nb_delays_60_90_2y],
[Nb_delays_90__2y], [Own_active_accounts_nb], [Max_overdue],
[RCC_credit_limit], [rcc_outstanding_amount], [Nb_active_mortgages],
[Nb_active_microcredits], [Total_mortgages], [Total_microcredits],
[Nb_active_consumer_credit], [Nb_active_rcc], [Total_consumer_credit],
[Total_rcc], [Is_FICO], [Fico_score_v2], [Fico_score_v3], [Is_SocialLink],
[SocialLink])
VALUES (NULL, @0, @1, @2, @3, @4, NULL, @5, @6, @7, @8, @9, NULL, @10, @11,
@12, @13, @14, @15, @16, @17, @18, @19, @20, NULL, NULL, NULL, @21, @22,
NULL, NULL, @23, @24, @25, @26, @27, NULL, @28, NULL, NULL)
SELECT [ID]
FROM [dbo].[NBKI_Response]
WHERE @@ROWCOUNT > 0 AND [ID] = scope_identity()
这不会为我在EF 6.1或EF Core 2上重新编程 乙二醇
使用系统;
使用System.ComponentModel.DataAnnotations.Schema;
使用System.Data.Entity;
命名空间Ef6Test
{
公开课Foo
{
公共int Id{get;set;}
[列(“Nb_延迟90+)]
公共int?Nb_延迟_90____曾经{get;set;}
}
公共类Db:DbContext
{
公共DbSet Foos{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
}
}
班级计划
{
静态void Main(字符串[]参数)
{
SetInitializer(新的DropCreateDatabaseAlways());
使用(var db=new db())
{
db.Database.Log=m=>Console.WriteLine(m);
db.Database.Initialize(true);
var f=新的Foo();
f、 Nb_延迟_90__曾经=2;
db.Foos.Add(f);
db.SaveChanges();
}
Console.WriteLine(“按任意键退出”);
Console.ReadKey();
}
}
}
是否可以使用不包含特殊字符的列名?很抱歉,我无法更改数据库…2017-11-01 20:00:22974错误NBKI_响应添加错误-System.Data.Entity.Infrastructure.DbUpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.Entity.Core.UpdateException:更新条目时出错。有关详细信息,请参见内部异常。-->System.Data.SqlClient.SqlException:列名“Nb\u延迟\u 90\u曾经”无效。列名“Nb_delays_90_2y”无效。“Nb_delays_90_2y ever”映射有问题。OnModelCreating中有什么内容吗?只有public virtual DbSet NBKI_Response{get;set;}
在OnModelCreating NBKI_Response中没有使用您需要从我的示例开始,添加内容直到失败,或者从你的项目开始,删除一些东西,直到它工作为止。非常感谢你——在所有情况下,你的例子都工作得很好——所以错误的原因是不同的!
INSERT [dbo].[NBKI_Response]([External_Data_Id], [Total_accounts],
[Total_negative_accounts], [Total_active_accounts], [Total_credit_limit],
[Total_overdue_amount], [Total_outstanding_amount],
[Total_installment_amount], [Total_disputed], [Total_letigation],
[Total_bankruptcy], [Total_inquiries], [Recent_inquiries],
[Recent_account_date], [Oldest_account_date], [Nb_delays_5_30_ever],
[Nb_delays_30_60_ever], [Nb_delays_60_90_ever], [Nb_delays_90__ever],
[Nb_delays_5_30_2y], [Nb_delays_30_60_2y], [Nb_delays_60_90_2y],
[Nb_delays_90__2y], [Own_active_accounts_nb], [Max_overdue],
[RCC_credit_limit], [rcc_outstanding_amount], [Nb_active_mortgages],
[Nb_active_microcredits], [Total_mortgages], [Total_microcredits],
[Nb_active_consumer_credit], [Nb_active_rcc], [Total_consumer_credit],
[Total_rcc], [Is_FICO], [Fico_score_v2], [Fico_score_v3], [Is_SocialLink],
[SocialLink])
VALUES (NULL, @0, @1, @2, @3, @4, NULL, @5, @6, @7, @8, @9, NULL, @10, @11,
@12, @13, @14, @15, @16, @17, @18, @19, @20, NULL, NULL, NULL, @21, @22,
NULL, NULL, @23, @24, @25, @26, @27, NULL, @28, NULL, NULL)
SELECT [ID]
FROM [dbo].[NBKI_Response]
WHERE @@ROWCOUNT > 0 AND [ID] = scope_identity()
using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace Ef6Test
{
public class Foo
{
public int Id { get; set; }
[Column("Nb_delays_90+_ever")]
public int? Nb_delays_90__ever { get; set; }
}
public class Db : DbContext
{
public DbSet<Foo> Foos { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<Db>());
using (var db = new Db())
{
db.Database.Log = m => Console.WriteLine(m);
db.Database.Initialize(true);
var f = new Foo();
f.Nb_delays_90__ever = 2;
db.Foos.Add(f);
db.SaveChanges();
}
Console.WriteLine("Hit any key to exit");
Console.ReadKey();
}
}
}