3

I know that there is a lot of similar questions but none of them helped me. I have a table 'DeliveryAddresses' with 'DeliveryAddressId' key (Guid type). It's marked as autogenerated [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] but when I'm trying to add new record to my table exception appears:

SqlException: Cannot insert the value NULL into column 'DeliveryAddressId', table 'ProjectName.dbo.DeliveryAddresses'; column does not allow nulls. INSERT fails.
The statement has been terminated.

I don't know what to do because in the same way I'm adding rows to other tables and everything looks the same.

public class DeliveryAddress
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid DeliveryAddressId { get; set; }

    [Required(ErrorMessage = "Number of the building is required")]
    public string StreetName { get; set; }

    [Required(ErrorMessage = "Postcode is required")]
    public string Postcode { get; set; }

    [Required(ErrorMessage = "City is required")]
    public string City { get; set; }
}

_

public class Product
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid ProductId { get; set; }

    [Display(Name = "Product")]
    [Required(ErrorMessage = "Product name is required")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Price is required")]
    public double Price { get; set; }

    [ForeignKey("Subcategory")]
    public Guid SubcategoryId { get; set; }
    public virtual Subcategory Subcategory { get; set; }
}

and this code works for Product, not for DeliveryAddress

Product prod = new Product();
prod.Name = "name";
prod.Price = 10.50;
prod.SubcategoryId = _dbContext.Subcategories.FirstOrDefault(s => s.SubcategoryId != null).SubcategoryId;
_dbContext.Products.Add(prod);
_dbContext.SaveChanges();
DeliveryAddress add = new DeliveryAddress();
add.City = "City";
add.StreetName = "Street";
add.Postcode = "55555";
_dbContext.DeliveryAddresses.Add(add);
_dbContext.SaveChanges();

It doesn't work even with add.DeliveryAddressId = Guid.NewGuid().

Only clue I can give you: when I was creating models by mistake I named this class 'DelieveryAddress' (extra 'e') but it's fixed in next migrations, maybe it's something about it?

Buh Buh
  • 7,443
  • 1
  • 34
  • 61
Dre
  • 43
  • 6
  • I don't think you can have an identity GUID (unique identifier) column in SQL server. you have to generate de GUID client or with a default value expression in SQL – Pedro Figueiredo Nov 09 '17 at 09:50
  • But it works for 'Product' :( – Dre Nov 09 '17 at 09:52
  • Yes. sorry didn't get that. my bad. can you give me details on how the columns are defined in the SQLServer? if entity framework generated a default value or something? – Pedro Figueiredo Nov 09 '17 at 09:54
  • Do u use DB-first or Model-first method? – Timur Lemeshko Nov 09 '17 at 09:57
  • Obviously, `ProductId` has a default constraint in the database, `DeliveryAddressId` not. – Gert Arnold Nov 09 '17 at 09:57
  • Could you show us a sql scripts for your Product and DeliveryAddress tables? – Roman Koliada Nov 09 '17 at 09:57
  • You mean values in columns with Guid type? 25FCC0B2-2FC5-E711-A2A2-54E1AD268057, 26FCC0B2-2FC5-E711-A2A2-54E1AD268057, 27FCC0B2-2FC5-E711-A2A2-54E1AD268057 etc – Dre Nov 09 '17 at 09:58
  • 2
    You need to check database table. The PK column should have something like `DEFAULT (newid())`. Normally EF migrations do that automatically. – Ivan Stoev Nov 09 '17 at 09:59
  • I'm using Model-first – Dre Nov 09 '17 at 10:00
  • No, in design view of the table in Management Studio the property Default Value for each Column `ProductId` and DeliveryAddressId` - in fact one will have `newid()` and the other don't the question is why. – Pedro Figueiredo Nov 09 '17 at 10:00
  • 1
    Uh, for ProductId it's (newsequentialid()), for DeliveryAddressId it's empty... but why? I was doing everything in the same way – Dre Nov 09 '17 at 10:02
  • Are you using migrations? Could you compare generated EF migrations for `ProdcutId` and `DeliveryAddressId`? – Roman Koliada Nov 09 '17 at 10:05
  • Do you see any difference in the Migration statements used to generate the tables? I would correct it there, but that will fix the problem without giving a why that happens in the first place. – Pedro Figueiredo Nov 09 '17 at 10:05
  • Yes, ProductId = c.Guid(nullable: false, identity: true), DelieveryAddressId = c.Guid(nullable: false), but again, why? In model classes everything looks the same... – Dre Nov 09 '17 at 10:08
  • `DelieveryAddressId = c.Guid(nullable: false)` - `Delievery`- It's a typo or you have an outdated migrations? – Roman Koliada Nov 09 '17 at 10:10
  • It's a typo, it's fixed in next migration `DeliveryAddressId = c.Guid(nullable: false),`, sorry, I should give you this one – Dre Nov 09 '17 at 10:14
  • 2
    Maybe it's better to regenerate your DB? See this: https://stackoverflow.com/q/16035333/5126411 – Roman Koliada Nov 09 '17 at 10:17
  • 2
    It worked! `DeliveryAddressId = c.Guid(nullable: false, identity: true),` Wow, thank you very much. Do you know what could cause this problem? – Dre Nov 09 '17 at 10:29
  • Glad to here you get it. I'm not sure, maybe renaming – Roman Koliada Nov 09 '17 at 10:30
  • I think it's because when I created this class it didn't have `[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]` attributes, it was added later – Dre Nov 09 '17 at 10:45

0 Answers0