Creating generator/trigger on identity column on creating database by Entity 
Framework
--------------------------------------------------------------------------------------

                 Key: DNET-477
                 URL: http://tracker.firebirdsql.org/browse/DNET-477
             Project: .NET Data provider
          Issue Type: Improvement
          Components: Entity Framework support
            Reporter: Alexey Milv
            Assignee: Jiri Cincura


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


-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://tracker.firebirdsql.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

------------------------------------------------------------------------------
LogMeIn Rescue: Anywhere, Anytime Remote support for IT. Free Trial
Remotely access PCs and mobile devices and provide instant support
Improve your efficiency, and focus on delivering more value-add services
Discover what IT Professionals Know. Rescue delivers
http://p.sf.net/sfu/logmein_12329d2d
_______________________________________________
Firebird-net-provider mailing list
Firebird-net-provider@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/firebird-net-provider

Reply via email to