Stumbled into another error. I have a model that looks like this:
public class LinkedAccount
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
[MaxLength(128)]
public string CreatedBy { get; set; }
[MaxLength(40)]
public string DisplayName { get; set; }
public LinkedAccountType AccountType { get; set; }
}
That translated into the following SQL after migrations:
CREATE TABLE dbo."LinkedAccounts"
(
"Id" uuid NOT NULL DEFAULT uuid_generate_v4(),
"CreatedBy" character varying(128),
"DisplayName" character varying(40),
"AccountType" integer NOT NULL DEFAULT 0,
CONSTRAINT "PK_dbo.LinkedAccounts" PRIMARY KEY ("Id")
)
WITH (
OIDS=FALSE
);
When I try to add a new row like this:
ApplicationDbContext.LinkedAccounts.Add(new LinkedAccount
{
CreatedBy = User.Identity.GetUserId(),
AccountType = LinkedAccountType.Store,
DisplayName = accountInfo.DisplayName
});
ApplicationDbContext.SaveChanges();
An exception is thrown:
A null store-generated value was returned for a non-nullable member 'Id' of type 'StoreFront.Models.LinkedAccounts'.
I've been hammering at this for 5 hours now. At first, I was not able to update-database (with error "uuid_generate_v4() was not found") despite the fact that uuid-ossp was installed with 'CREATE EXTENSION "uuid-ossp" WITH SCHEMA dbo;'. Only after re-creating the "public" schema and then doing 'CREATE EXTENSION "uuid-ossp";' did the uuid_generate_v4() function start to work. Doing a "SELECT uuid_generate_v4();" shows that proper UUIDs are being generated, but when inserting a row through Entity Framework, only 0's are produced for the Id column. Getting rid of the [DatabaseGenerated(...)] attribute allows one row to be filled in with a UUID of all 0's.
I can maybe get around this by producing GUIDs in code (Guid.NewGuid()), but I would much rather prefer letting the database handle it. Any ideas?
ApplicationDbContext.LinkedAccounts.Add(new LinkedAccount
{
Id = Guid.NewGuid(),
CreatedBy = User.Identity.GetUserId(),
AccountType = LinkedAccountType.Amazon,
DisplayName = accountInfo.DisplayName
});
I get the error: "The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database."
I am already using Code First migrations with -ContextTypeName set to StoreFront.Models.ApplicationDbContext (I even enabled migrations again with the -force argument). When I add-migrations, it shows that nothing has changed with my database (meaning ApplicationDbContext should be the same?) Anyway, I got around this by adding the following line to Application_Start():
Database.SetInitializer<ApplicationDbContext>(null);
And now I can insert rows into the database. However, this patchy/hacky solution is far from ideal. Any ideas?