Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating generator/trigger on identity column on creating database by Entity Framework [DNET477] #161

Closed
firebird-automations opened this issue Dec 24, 2012 · 2 comments

Comments

@firebird-automations
Copy link

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; }
}

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\(\);
    \}
\}

}

@firebird-automations
Copy link
Author

Commented by: @cincuranet

The `SSDLToFB.tt` was eventually replaced by EF Migrations.

@firebird-automations
Copy link
Author

Modified by: @cincuranet

status: Open [ 1 ] => Closed [ 6 ]

resolution: Incomplete [ 4 ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants