具有名称Nb_delays_90 + _ever的Column属性出错(具有特殊字符)

时间:2017-11-01 16:27:26

标签: c# sql-server entity-framework ef-code-first

我有Code First表类,字段为:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[NBKI_Response](
[ID] [int] IDENTITY(1,1) NOT NULL,
[External_Data_Id] [int] NULL,
[Total_accounts] [int] NULL,
[Total_negative_accounts] [int] NULL,
[Total_active_accounts] [int] NULL,
[Total_credit_limit] [int] NULL,
[Total_overdue_amount] [int] NULL,
[Total_outstanding_amount] [int] NULL,
[Total_installment_amount] [int] NULL,
[Total_disputed] [int] NULL,
[Total_letigation] [int] NULL,
[Total_bankruptcy] [int] NULL,
[Total_inquiries] [int] NULL,
[Recent_inquiries] [int] NULL,
[Recent_account_date] [datetime] NULL,
[Oldest_account_date] [datetime] NULL,
[Nb_delays_5_30_ever] [int] NULL,
[Nb_delays_30_60_ever] [int] NULL,
[Nb_delays_60_90_ever] [int] NULL,
[Nb_delays_90+_ever] [int] NULL,
[Nb_delays_5_30_2y] [int] NULL,
[Nb_delays_30_60_2y] [int] NULL,
[Nb_delays_60_90_2y] [int] NULL,
[Nb_delays_90+_2y] [int] NULL,
[Own_active_accounts_nb] [int] NULL,
[Max_overdue] [numeric](18, 2) NULL,
[RCC_credit_limit] [numeric](18, 2) NULL,
[rcc_outstanding_amount] [numeric](18, 2) NULL,
[Nb_active_mortgages] [int] NULL,
[Nb_active_microcredits] [int] NULL,
[Total_mortgages] [int] NULL,
[Total_microcredits] [int] NULL,
[Nb_active_consumer_credit] [int] NULL,
[Nb_active_rcc] [int] NULL,
[Total_consumer_credit] [int] NULL,
[Total_rcc] [int] NULL,
[Is_FICO] [bit] NULL,
[Fico_score_v2] [int] NULL,
[Fico_score_v3] [int] NULL,
[Is_SocialLink] [bit] NULL,
[SocialLink] [nvarchar](max) NULL,
CONSTRAINT [PK_NBKI_Response] PRIMARY KEY CLUSTERED 
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

我在尝试添加新记录时遇到错误“System.Data.SqlClient.SqlException:列名无效”。

为什么以及如何修复?

在数据库列中,名称正好为“Nb_delays_90 + _ever”......

UPD:我不允许在数据库中更改列名

UPD2:表格的脚本(我使用现有数据库中的Code First):

db.Database.Log = m => Console.WriteLine(m);

UPD3 添加跟踪后:

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

我看到按列属性的映射不起作用:

Public Class Form1
    Private numbers As New List(Of String) From {"Zero", "One", "Two", "Three", "Four", _
      "Five", "Six", "Seven", "Eight", "Nine", "Ten"}

    Private Sub btnPomeranz_Click(sender As Object, e As EventArgs) _
      Handles btnPomeranz.Click
        Dim number, bacon As Integer

        If Not Integer.TryParse(txtKershaw.Text, number) Then
            MessageBox.Show(txtKershaw.Text & " is not an integer")
            Exit Sub
        End If

        Do
            number = Count(number)
            If number = 0 Then
                MessageBox.Show("The number " & txtKershaw.Text & " was not recognised")
                Exit sub
            End If

            bacon += 1
            If bacon > 100 Then
                MessageBox.Show("More than 100 steps required")
                Exit Sub
            End If
        Loop Until number = 4
        lblKluber.Text = bacon.ToString
    End Sub

    Private Function Count(number As Integer) As Integer
        If number < 0 Or number >= numbers.Count Then Return 0
        Return numbers(number).Length
    End Function
End Class

1 个答案:

答案 0 :(得分:1)

对于我来说,无论是在EF 6.1还是EF Core 2上,我都不会这样做。

例如

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