StoreGeneratedPattern, DefaultValue and fluent mapping
Posted: Tue 30 Aug 2011 06:38
The technique described in this blog post doesn't seem to work with the DbContext generated by Entity Developer when fluent mapping is enabled. I noticed that the example uses ObjectContext, but I've verified that it works fine with DbContext when fluent mapping is disabled, which I guess is because it finds the specified default values in the XML mapping files. Is this a known limitation, a bug, or is there something else I'm missing?
The example in the link produces the following SQL with ObjectContext, or with DbContext without fluent mapping:
And here's the generated (failing) SQL with fluent mapping enabled:
The latter SQL statement, for obvious reasons, gives a DbUpdateException with the message: {"ORA-01400: cannot insert NULL into (\"INFO\".\"PRODUCTS\".\"Id\")\nORA-06512: at line 4"}
Here's the (auto-generated) fluent mapping code I'm using:
The example in the link produces the following SQL with ObjectContext, or with DbContext without fluent mapping:
Code: Select all
DECLARE
updatedRowid ROWID;
BEGIN
INSERT INTO INFO.PRODUCTS("Id", "FunctionGeneratedValue", "ProductName", "Price", "ModifiedBy", "LastModified")
VALUES (MY_SEQUENCE.NEXTVAL, MY_FUNCTION(), :p0, :p1, USER, current_timestamp)
RETURNING ROWID INTO updatedRowid;
OPEN :outParameter FOR SELECT "Id", "FunctionGeneratedValue" FROM INFO.PRODUCTS WHERE ROWID = updatedRowid;
END;
Code: Select all
DECLARE
updatedRowid ROWID;
BEGIN
INSERT INTO INFO.PRODUCTS("ProductName", "Price")
VALUES (:p0, :p1)
RETURNING ROWID INTO updatedRowid;
OPEN :outParameter FOR SELECT "Id", "FunctionGeneratedValue", "ModifiedBy", "LastModified" FROM INFO.PRODUCTS WHERE ROWID = updatedRowid;
END;
Here's the (auto-generated) fluent mapping code I'm using:
Code: Select all
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
#region Products
modelBuilder.Entity()
.HasKey(p => new { p.Id })
.ToTable("PRODUCTS", "INFO");
// Properties:
modelBuilder.Entity()
.Property(p => p.Id)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity()
.Property(p => p.ProductName)
.IsRequired()
.HasMaxLength(160)
.IsUnicode(false);
modelBuilder.Entity()
.Property(p => p.Price);
modelBuilder.Entity()
.Property(p => p.FunctionGeneratedValue)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity()
.Property(p => p.ModifiedBy)
.IsRequired()
.HasMaxLength(30)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed)
.IsUnicode(false);
modelBuilder.Entity()
.Property(p => p.LastModified)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
#endregion
#region Disabled conventions
modelBuilder.Conventions.Remove();
modelBuilder.Conventions.Remove();
#endregion
}