0

I'm sure that I'm just missing something obvious but I can't seem to get the following code to function correctly.

This is one of my POCO classes that uses a Guid:

public class Player
{
    public virtual Guid PlayerID { get; set; }
    public virtual int? GuildID { get; set; }

    public virtual int AccountNumber { get; set; }
    public virtual string UserName { get; set; }

    public virtual Guild Guild { get; set; }

    public virtual ICollection<Coordinate> Coordinates { get; set; }
}

It is linked to it's own configuration class using the Fluent API:

public class PlayerConfiguration : EntityTypeConfiguration<Player>
{
    public PlayerConfiguration()
    {
        ToTable("Players", "ACDB");

        Property(p => p.PlayerID)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(p => p.UserName).HasMaxLength(25);
    }
}

The configuration is then registered with the DbContext.

The error I'm getting is the following: Incorrect column specifier for column 'PlayerID'

Any ideas on where I'm going wrong?

nick_w
  • 14,758
  • 3
  • 51
  • 71
Arcadian
  • 1,373
  • 4
  • 20
  • 30

1 Answers1

1

What I think is happening here is that the Entity Framework creates the PlayerID column as a GUID (UUID in MySql) and then applies the AUTO_INCREMENT attribute to it.

From http://docs.oracle.com/cd/E17952_01/refman-5.5-en/numeric-type-attributes.html it seems that AUTO_INCREMENT is valid only for integer columns, which may be why you are receiving this error.

I think the thing to try here is to change PlayerID from a Guid to an int - this should at least narrow down the cause of the error.

Edit 1

From Convert and modify a model field as an DataColumn object and Disabling identity (auto-incrementing) on integer primary key using code first you should be able to turn off the auto incrementing with:

HasKey(p => p.PlayerID)
Property(p => p.PlayerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)

Edit 2

At least on SQL Server, using Property(p => p.PlayerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None) will result in an empty Guid being inserted when a new row is created (though it seems reasonable to assume it would behave this way elsewhere). The only sensible workaround I can see is to populate the PlayerID in the Player constructor:

public Player()
{
    this.PlayerID = Guid.NewGuid();
}
Community
  • 1
  • 1
nick_w
  • 14,758
  • 3
  • 51
  • 71
  • It does work as an int, I've tested that. I would however, prefer to use GUIDs/UUIDs. Is there a way to prevent the AUTO INCREMENT switch being turned on? Or is it one of the things still missing from Code First like being able to set a max length on int values? In any case, according to Julia Lerman's Programming Entity Framework: Code First, this method works with the local SQL express server and SQL server CE. Could it be the fault of MySQL's .Net provider perhaps? – Arcadian Nov 01 '12 at 00:27
  • But if I do that then the database will not know that it has to generate the GUID, at least that is how I understand it. It would just insert a zero value GUID. – Arcadian Nov 01 '12 at 01:22
  • I believe the only available option is the one in your second edit so I'll mark it as correct. Still this does seem like unintended behaviour but I'm not sure if this is an EF5 issue or a MySQL .NET provider issue. It would be nice to see if this ever get's fixed. – Arcadian Nov 01 '12 at 02:07