You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When creating database via Code first (not sure about model first) with primary key set to identity FB provider creates tables and set's the comment but doesn't auto creates generators/triggers for identity columns that makes using Code First tricky.
Patch: (possible makes sense to try to drop generator/trigger first, but I doubt that)
--- http://SSDLToFB.tt Wed Aug 1 11:23:23 2012
+++ SSDLToFB.new Mon Dec 24 13:20:00 2012
@@ -51,6 +51,17 @@
{
#>
COMMENT ON COLUMN <#=Quote(TableName(entitySet))#>.<#=Quote(comment.Key)#> IS '<#=comment.Value#>';
+CREATE GENERATOR <#=Quote("GEN_"+TableName(entitySet)+"_ID")#>;
+SET TERM ^ ;
+CREATE TRIGGER <#=Quote(TableName(entitySet)+"_BI")#> for <#=Quote(TableName(entitySet))#>
+ACTIVE BEFORE INSERT POSITION 0
+AS
+BEGIN
+ IF (NEW.<#=Quote(comment.Key)#> IS NULL) THEN
+ NEW.<#=Quote(comment.Key)#> = GEN_ID(<#=Quote("GEN_"+TableName(entitySet)+"_ID")#>,1);
+END
+^
+SET TERM ; ^
<#
}
}
Example: (use EF 5, set connection string to valid server and DB to nonexistent database)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using FirebirdSql.Data.FirebirdClient;
using System.ComponentModel.DataAnnotations.Schema;
namespace FB_CF
{
public class Item
{
public int ItemID { get; set; }
public string ItemText { get; set; }
}
public class Item2
\{
public int Item2AA \{ get; set; \}
\}
class Program
\{
public class Context : DbContext
\{
public Context\(\) : base\(new FbConnection\("server type=1;initial catalog=c:\\\\soft\\\\DB\.FDB;user id=SYSDBA;password=masterkey;clientlibrary=c:\\\\soft\\\\fbembed\.dll"\), true\) \{ \}
public DbSet<Item\> Items \{ get; set; \}
public DbSet<Item2\> Item2s \{ get; set; \}
//public DbSet<LessonInfo\> LessonInfos \{ get; set; \}
protected override void OnModelCreating\(DbModelBuilder modelBuilder\)
\{
modelBuilder\.Entity<Item\>\(\)\.HasKey\(p =\> p\.ItemID\)\.Property\(p =\> p\.ItemID\)\.HasDatabaseGeneratedOption\(DatabaseGeneratedOption\.Identity\);
modelBuilder\.Entity<Item2\>\(\)\.HasKey\(p =\> p\.Item2AA\)\.Property\(p =\> p\.Item2AA\)\.HasDatabaseGeneratedOption\(DatabaseGeneratedOption\.None\);
\}
\}
static void Main\(string\[\] args\)
\{
Context x = new Context\(\);
Item tt = new Item\(\);
tt\.ItemText = "test";
x\.Items\.Add\(tt\);
Item2 tt2 = new Item2\(\);
tt2\.Item2AA = 2;
x\.Item2s\.Add\(tt2\);
x\.SaveChanges\(\);
\}
\}
}
The text was updated successfully, but these errors were encountered:
Submitted by: Alexey Milov (elv)
Votes: 3
When creating database via Code first (not sure about model first) with primary key set to identity FB provider creates tables and set's the comment but doesn't auto creates generators/triggers for identity columns that makes using Code First tricky.
Patch: (possible makes sense to try to drop generator/trigger first, but I doubt that)
--- http://SSDLToFB.tt Wed Aug 1 11:23:23 2012
+++ SSDLToFB.new Mon Dec 24 13:20:00 2012
@@ -51,6 +51,17 @@
{
#>
COMMENT ON COLUMN <#=Quote(TableName(entitySet))#>.<#=Quote(comment.Key)#> IS '<#=comment.Value#>';
+CREATE GENERATOR <#=Quote("GEN_"+TableName(entitySet)+"_ID")#>;
+SET TERM ^ ;
+CREATE TRIGGER <#=Quote(TableName(entitySet)+"_BI")#> for <#=Quote(TableName(entitySet))#>
+ACTIVE BEFORE INSERT POSITION 0
+AS
+BEGIN
+ IF (NEW.<#=Quote(comment.Key)#> IS NULL) THEN
+ NEW.<#=Quote(comment.Key)#> = GEN_ID(<#=Quote("GEN_"+TableName(entitySet)+"_ID")#>,1);
+END
+^
+SET TERM ; ^
<#
}
}
Example: (use EF 5, set connection string to valid server and DB to nonexistent database)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using FirebirdSql.Data.FirebirdClient;
using System.ComponentModel.DataAnnotations.Schema;
namespace FB_CF
{
public class Item
{
public int ItemID { get; set; }
public string ItemText { get; set; }
}
}
The text was updated successfully, but these errors were encountered: