EntityFramework database for the first type - binary map (8) from SQL to int in C #

Inside SQL, I have a table with a primary key as binary (8). When I add this table to my model with Update Model from Database, I see that this column is of type =Binary

enter image description here

and in C # I get this column as byte[].

Is it possible to map this column to int?

I know that I can create a view with CASTin SQL:

SELECT
    Client_Id,
    CAST(Client_Id AS INT) AS NewClient_Id,
    * /*other columns*/
FROM
    dbo.Clients

but this is not a solution, because I must be able to write, and not just read from this table. I know that I can create a stored procedure for inserts, but I would like to avoid this.

I am using EntityFramewor 6.1.3.

+4
source share
4 answers

3

x , .

x . , , , . , ( ).

[NotMapped]
public long LongClientId
{
    get { return BitConverter.ToInt64(this.ClientId, 0); }
    set { this.ClientId = BitConverter.GetBytes(value); }
}

context.MyDbSet.Where(m => m.LongClientId == 12).ToList();

context.MyDbSet.ToList().Where(m => m.LongClientId == 12);

, ( dbms ) , , .

x (, ) INSTEAD OF.

+2

, int, , #

0

, .

long CurrentClientId = BitConverter.ToInt64(Rec.ClientId) 

Rec.ClientId = BitConverter.GetBytes(CurrentClientId) 

. , , , .

, , , , , , , .

0

:

    [NotMapped]
    public long ClientId
    {
        get { return BitConverter.ToInt64(this.ClientIdBytes, 0); }
        set { this.ClientIdBytes = BitConverter.GetBytes(value); }
    }

    [Column("ClientId")]
    public byte[] ClientIdBytes { get; set; }

ClientId , . , ClientId.

0

Source: https://habr.com/ru/post/1649500/


All Articles