Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error 2019: Member Mapping specified is not valid using Entity Framework Code First

I am implementing Entity Framework Code-First methodology in my project and got stuck in an issue.

Error Details: 18,12) : error 2019: Member Mapping specified is not valid. The type 'Edm.Byte[Nullable=False,DefaultValue=]' of member 'status' in type 'IMS.DAL.Users_mast' is not compatible with 'SqlServer.binary[Nullable=False,DefaultValue=,MaxLength=8000,FixedLength=True]' of member 'status' in type 'CodeFirstDatabaseSchema.Users_mast'.

My Code: User_mast.cs file contains:

[Column(TypeName = "binary")]
public byte status { get; set; } // Active | Inactive (1 | 0 ), where I want status column to be created as binary column in sql server.

public class context : DbContext
{
    public context()
        : base("name=sqlConn")
    {
    }

    public DbSet<Users_mast> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

and finally,

    context obj = new context();

    public void add()
    {
        obj.Users.Add(new Users_mast() {user_fname="Agnib", user_lname="Pyne", user_email="[email protected]", user_mobile="9830972301", username="agnib", pwd="As1232", created_date=DateTime.Now, status=1 });
        obj.SaveChanges();
    }

On running, I am getting the above error. I can understand its due to the status field set as byte which is not getting mapped as binary.

Then what should I do to make the column in sql server as binary? Is there any list of mapping between sql server types and .net types specially in case of EF Code-First?

Any help is appreciated. Please help!!!

like image 206
Agnib Avatar asked Jun 21 '26 02:06

Agnib


1 Answers

Change your property to:

[Column(TypeName = "bit")]
public bool status { get; set; }

The native C# type bool is specifically created to store true and false values. The bool datatype maps to the SQL bit datatype.

The binary SQL datatype is meant to store binary data like files or images. If you want to map to a C# property you could use a byte[] datatype (array of bytes). In that case the code would look something like this:

[Column(TypeName = "binary")]
public byte[] status { get; set; }
like image 89
Maurits van Beusekom Avatar answered Jun 22 '26 16:06

Maurits van Beusekom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!