0

For the first time ever I'm using GUIDs for PK values in my POCO classes which has presented a rather irritating problem being that I can't seem to get data added to my tables.

Here's an example addition:

public partial class EntityType
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public string Type { get; set; }

    public bool Deleted { get; set; }
}

Here's my migration configuration seed method:

internal sealed class Configuration : DbMigrationsConfiguration<AC.WebGUI.Models.OrtundDataModel>
{
    protected override void Seed(AC.WebGUI.Models.OrtundDataModel context)
    {
        // Prepopulate Entity Types.
        if (!context.EntityTypes.Any())
        {
            context.EntityTypes.AddOrUpdate(
                new EntityType { Id = Guid.NewGuid(), Type = "Supplier" },
                new EntityType { Id = Guid.NewGuid(), Type = "Distributor" },
                new EntityType { Id = Guid.NewGuid(), Type = "Staff" },
                new EntityType { Id = Guid.NewGuid(), Type = "Customer" },
                new EntityType { Id = Guid.NewGuid(), Type = "Pharmacy" }
            );
            context.SaveChanges();
        }            
    }

Note that in both classes, I'm specifying a Guid.NewGuid() value for my Id property.

This, however, has not got me around the error:

System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Id', table 'Ortund.dbo.EntityTypes'; column does not allow nulls. INSERT fails.

Obviously calling Guid.NewGuid() can't be a null value so I'm very unsure of how to proceed at this point. I figured that if I added it to the constructor on the class that defines the table, I could remove it from the actual implementation of that class, however, whether I take it out of the constructor or out of the implementation, the error persists.

How can I get these Guids added to my table data without any errors? What am I missing?

Edit - Addressing duplicate question

Since my question has been marked as a duplicate of this question which deals with using the specified data annotation to make EF automatically generate the GUID value for the PK column on the table, I thought I'd just explain how mine is different.

The idea is the same, true enough, but the implementation has diverged from what is discussed in the other question so I've arrived at a totally different solution for my own project which involved removal of this data annotation whereas the solutions proposed in the other question keep it and make other changes to the data model.

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Possible duplicate of [Entity Framework 6 GUID as primary key: Cannot insert the value NULL into column 'Id', table 'FileStore'; column does not allow nulls](https://stackoverflow.com/questions/23081096/entity-framework-6-guid-as-primary-key-cannot-insert-the-value-null-into-column) – Tanner Jun 13 '17 at 15:11
  • 3
    What happens if you remove [DatabaseGenerated(DatabaseGeneratedOption.Identity)] from EntityType? That attribute tells the db to generate your PK for you but shouldn't be needed if you're doing it yourself. – Adam Benson Jun 13 '17 at 15:13
  • Should you be setting DatabaseGenerated(DatabaseGeneratedOption.Identity) and supplying a value for it at the same time? – AnthonyJClink Jun 13 '17 at 15:20
  • @AdamBenson but the database doesn't generate its own guid values except through newid() / newsequentialid() which isn't what this does, does it? – Ortund Jun 14 '17 at 06:50

1 Answers1

0

Figured I'd answer this before it gets closed.

Per comments, simply removing the DatabaseGenerated(DatabaseGeneratedOption.Identity)] annotation from the PK property fixed the problem though I did keep the assignment in the constructor.

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • 1
    This is because EF won't pass the GUID value when the database generated option is identity because it suspects it to be given my dbms - if you would have looked at the generated sql, you would have seen the guid was not even mentioned in the insert statements. – DevilSuichiro Jun 14 '17 at 07:38