In Entity Framework, set the model attribute to boolean.

entity-framework

Question

I don't have a lot of expertise with ORMs, and I'm just beginning to understand a little bit about the entity framework.

I have one table in my little application, and it includes multiple columns, including a PrimaryKey (int), a Name (string), and a Flag (tinyint).

The datatype of the Flags column was automatically set to a byte when I imported this database. This is good, however the Flag need to be a boolean instead, so I

  1. pressed the Mapping Details button.
  2. choose my Flag's home
  3. swapped out the Byte type for a Boolean one.
  4. rebuilt the program

This error then appeared:

Error 2019: Member Mapping specified is not valid. The type 'Edm.Boolean[Nullable=True,DefaultValue=]' of member 'MyFlag' in type 'MyModel.MyItem' is not compatible with 'SqlServer.tinyint[Nullable=True,DefaultValue=]' of member 'MyFlag' in type 'MyModel.Store.MyItem'.

Is it possible to have

MyItem item = new MyItem();
item.Flag = true;

and instruct the database to store Flag as 1?

1
16
9/18/2009 6:05:30 PM

Accepted Answer

Changes to the datatype ofMyFlag to the database's bit.

18
10/9/2017 10:41:31 AM

Popular Answer

I believe for thetinyint To properly read from and write to that field, you must create a partial class and utilize a separate field. However, the framework will understand things properly.bit as fieldsboolean .

You might attempt the partial listed below.

public partial class MyItem
{
    public bool FlagBool
    {
        get { return Flag == 1; }
        set { Flag = value ? 1 : 0; }
    }
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow