0

I'm learning Entity Framework on Azure and the ID field is required to be nvarchar(255). When I attached to the DB to get the model it pulled down the ID field as type string which was giving me issues when inserting a new record. The solution was to switch the type string to type Guid. Then I could submit a new record and the "default" sql value would generate the new Guid.

However, after I went through the model and reset the id's from string to Guid I got the following message which isn't making a lot of sense to me:

Error 2019: Member Mapping specified is not valid. The type 'Edm.Guid[Nullable=False,DefaultValue=]' of member 'id' in type 'LearnModel.AdminList' is not compatible with 'SqlServer.nvarchar[Nullable=False,DefaultValue=,MaxLength=255,Unicode=True,FixedLength=False]' of member 'id' in type 'LearnModel.Store.AdminList'

I'm thinking it may be the Unicode=true that's giving me the issue but I don't see anywhere this can be set in the model within VS. I found another post that mentions going in and editing the edmx to add in the Unicode=true but this would get overwritten the next time the model is validated.

Anyone know what I'm running into here?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zonus
  • 2,313
  • 2
  • 26
  • 48
  • If you're using EDMX, try changing `id` column type to 'uniqueidentifier', then rebuild the model (may require deleting and re-adding EDMX file to generate correct data type). – Tetsuya Yamamoto Feb 02 '17 at 02:00
  • I'd accept this as the answer if I could! It was not a quick undertaking by any means... I also did have to delete the model and re-create it as you said. Thanks for your help. – Zonus Feb 02 '17 at 21:33

1 Answers1

2

I have run with similar issue before, which solved by these steps:

  1. Change id identity to uniqueidentifier on database side, either by table designer or SQL query like this:

    ALTER TABLE AdminList ALTER COLUMN id uniqueidentifier
    
  2. Open the contents of EDMX file in XML editor, find EntityType element which represents target table name, then change Property element for id column with same data type as defined in DB like below:

    <EntityType Name="AdminList">
        <Property Name="id" Type="uniqueidentifier" Nullable="False" />
    </EntityType>
    

    NB: You can use <Property StoreGeneratedPattern="Identity" /> if you want to avoid EF setting id column value when submitting records (see this post).

  3. Save and rebuild the model.

The solution above is applicable if you don't want to remove EDMX file for some reasons.

If the problem still persists after editing EDMX file, the easiest way to overcome is just delete all EDMX-related files (ModelName.edmx, ModelName.Context.cs, ModelName.tt etc.), then recreate EDMX file with current table schema.

Additional reference:

Updating Entity Framework Model

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
  • As a side note, I'm surprised that MS recommended I created the ID column as nvarchar(). It should be uniqueidentifier. – Zonus Feb 03 '17 at 16:12