Agnib
Agnib

Reputation: 99

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!!!

Upvotes: 3

Views: 25201

Answers (2)

m1m1k
m1m1k

Reputation: 1435

In my case, all columns were set correctly, but Oracle Unmanaged Driver apparently doesn't always load into visual studio quite right.

I could get it to work sometimes, but it often failed after restarting my visual studio development machine. I experimented with combinations of rebuilding + reloading T4 templates and Re-compiling files in the solution (MVC5 + EF + AngularJS + Bundled Web optimization). In the end, this turned out to be a more straight forward solution:

This: Error 2019: Member Mapping specified is not valid. | Oracle Community[^]

  • (a) Open Visual Studio Help/About Microsoft Visual Studio and click OK button to exit the dialog box
  • OR
  • (b) Open the to-be-used connection in Server Explorer

Upvotes: 0

Maurits van Beusekom
Maurits van Beusekom

Reputation: 5999

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

Upvotes: 3

Related Questions