简体   繁体   中英

Why can't I add new data to my database with Entity Framework Code First?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM