简体   繁体   中英

“Implicit conversion from data type nvarchar(max) to varbinary is not allowed” error when storing data using EF6

I have two model classes - both have a byte[] field for storing a PDF file.

In the database the two model classes correspond to two tables - both with a varbinary(max) column.

For the one model class, I can easily persist to SQL Server, however for the other class + table I get the error

Implicit conversion from data type nvarchar(max) to varbinary is not allowed

The two classes and tables are basically similar - except for the names.

I've looked at a lot of answers for for this error, but cannot find any that use EF6. I've created the database tables manually and the classes as well - so no auto-generation involved.

Hope someone can help me past this error.

Thanks in advance

Tried changing column type from varbinary(max) to nvarchar(max) .

Tried deleting table and creating from scratch

//Pdf 
   [Table("SelfBillings")]
public class SelfBilling
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long Id { get; set; }

    public string OwnerId { set; get; }

    public long BookingId { set; get; }

    public long SelfBillingNumber { set; get; }

    public DateTime Date { get; set; }

    public DateTime PeriodStart { set; get; }

    public DateTime PeriodEnd { set; get; }

    public decimal PeriodDays { get; set; }

    public decimal PeriodHours { get; set; }

    public decimal RentalAmount { get; set; }

    public decimal RentalAmountTotal { get; set; }

    public decimal VatAmount { get; set; }

    public decimal TotalAmount { get; set; }

    public decimal TotalAmountInclVat { get; set; }

    //Pdf             
    public byte[] Attachment { set; get; }        
}

//Database table
CREATE TABLE [dbo].[SelfBillings]
(
    [Id] [BIGINT] IDENTITY(1,1) NOT NULL,
    [OwnerId] [VARBINARY](128) NOT NULL,
    [BookingId] [BIGINT] NOT NULL,
    [SelfBillingNumber] [BIGINT] NOT NULL,
    [Date] [DATETIME2](7) NOT NULL,
    [PeriodStart] [DATETIME2](7) NOT NULL,
    [PeriodEnd] [DATETIME2](7) NOT NULL,
    [PeriodDays] [DECIMAL](10, 2) NOT NULL,
    [PeriodHours] [DECIMAL](10, 2) NOT NULL,
    [RentalAmount] [DECIMAL](10, 2) NOT NULL,
    [RentalAmountTotal] [DECIMAL](10, 2) NOT NULL,
    [VatAmount] [DECIMAL](10, 2) NOT NULL,
    [TotalAmount] [DECIMAL](10, 2) NOT NULL,
    [TotalAmountInclVat] [DECIMAL](10, 2) NOT NULL,
    [Attachment] [VARBINARY](MAX) NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

//Mapping
modelBuilder.Entity<SelfBilling>().ToTable("SelfBillings");

I expect to have a record created in table SelfBillings .

Attachment is NVARBINARY(MAX) but I wonder if it is complaining about:

[OwnerId] [VARBINARY](128) NOT NULL,

Begin mapped to

public string OwnerId { set; get; }

It's expecting a byte[] type, not string for this field likely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM