Created
May 30, 2012 01:52
-
-
Save sourishkrout/2832562 to your computer and use it in GitHub Desktop.
Modified .SaveChanges(...) to pre-populate primary keys.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Data.Common; | |
using System.Data.Entity; | |
using System.Data.Entity.Infrastructure; | |
using System.Linq; | |
using System.Text; | |
using Oracle.DataAccess.Client; | |
using EFTracingProvider; | |
using System.IO; | |
using System.ComponentModel.DataAnnotations; | |
namespace EFTest1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
DataTable dt = DbProviderFactories.GetFactoryClasses(); | |
// Global config switch to enable log to console. | |
EFTracingProviderConfiguration.LogToConsole = true; | |
using (var ctx = new Context()) | |
{ | |
try | |
{ | |
var obj = ctx.Stops.Find(4217); | |
List<Stop> stops = new List<Stop>(); | |
stops.Add(obj); | |
foreach (var s in stops) | |
{ | |
if (s.StopNames != null) | |
{ | |
List<StopName> snlist = new List<StopName>(s.StopNames); | |
Console.WriteLine(snlist[0].NameWithoutLocality); | |
StopName kirsche = new StopName() | |
{ | |
Id = 0, | |
NameWithLocality = "Kirsche", | |
NameWithoutLocality = "Kirsche", | |
MotId = null, | |
NecId = null, | |
ObId = 3116, | |
TypeId = null, | |
SeqNum = null, | |
NetVersionId = 130, | |
Stop = snlist[0].Stop | |
}; | |
//s.StopNames.Add(kirsche); | |
//ctx.SaveChanges("STOP_NAME"); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
} | |
} | |
} | |
} | |
public class IdentityObject | |
{ | |
public decimal Id { get; set; } | |
} | |
public class Stop : IdentityObject | |
{ | |
public string ShortName { get; set; } | |
public decimal NetVersionId { get; set; } | |
public short? IsCentralStop { get; set; } | |
virtual public ICollection<StopName> StopNames { get; set; } | |
} | |
public class StopName : IdentityObject | |
{ | |
public string NameWithLocality { get; set; } | |
public string NameWithoutLocality { get; set; } | |
public decimal? StopId { get; set; } | |
public decimal NetVersionId { get; set; } | |
public decimal? ObId { get; set; } | |
public decimal? MotId { get; set; } | |
public decimal? TypeId { get; set; } | |
public decimal? SeqNum { get; set; } | |
public decimal? NecId { get; set; } | |
virtual public Stop Stop { get; set; } | |
} | |
public class Context : DbContext | |
{ | |
public Context() | |
: base(Context.CreateTracingConnection("DATA SOURCE=DEV;PASSWORD=oha12;PERSIST SECURITY INFO=True;USER ID=NET_VVS_TEST", "Oracle.DataAccess.Client"), true) | |
{ | |
} | |
public DbSet<Stop> Stops { get; set; } | |
public DbSet<StopName> StopNames { get; set; } | |
protected override void OnModelCreating(DbModelBuilder modelBuilder) | |
{ | |
base.OnModelCreating(modelBuilder); | |
modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); | |
modelBuilder.Entity<Stop>().Property(s => s.ShortName).HasColumnName("STOP_SHORT_NAME"); | |
modelBuilder.Entity<Stop>().Property(s => s.NetVersionId).HasColumnName("STOP_NV_ID"); | |
modelBuilder.Entity<Stop>().Property(s => s.IsCentralStop).HasColumnName("STOP_IS_CENTRAL_STOP"); | |
modelBuilder.Entity<Stop>().Property(s => s.Id).HasColumnName("STOP_ID"); | |
modelBuilder.Entity<Stop>().HasKey(s => s.Id); | |
modelBuilder.Entity<Stop>().ToTable("P_STOP", "NET_VVS_TEST"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.ObId).HasColumnName("STPN_OB_ID"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.MotId).HasColumnName("STPN_MOT_ID"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.TypeId).HasColumnName("STPN_NAME_TYPE"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.SeqNum).HasColumnName("STPN_NAME_SEQ_NO"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.NecId).HasColumnName("STPN_NEC_ID"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.NameWithoutLocality).HasColumnName("STPN_NAME_WITHOUT_LOCALITY"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.NameWithLocality).HasColumnName("STPN_NAME_WITH_LOCALITY"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.StopId).HasColumnName("STPN_STOP_ID"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.NetVersionId).HasColumnName("STPN_NV_ID"); | |
modelBuilder.Entity<StopName>().Property(sn => sn.Id).HasColumnName("STPN_ID").HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); | |
modelBuilder.Entity<StopName>().HasKey(sn => sn.Id); | |
modelBuilder.Entity<StopName>().HasRequired(sn => sn.Stop).WithMany(s => s.StopNames).HasForeignKey(sn => sn.StopId); | |
modelBuilder.Entity<StopName>().ToTable("P_STOP_NAME", "NET_VVS_TEST"); | |
} | |
public static DbConnection CreateTracingConnection(string connectionStr, string providerInvariantName) | |
{ | |
return new OracleConnection(connectionStr); | |
string wrapperConnectionString = | |
String.Format(@"wrappedProvider={0};{1}", providerInvariantName, connectionStr); | |
EFTracingConnection connection = | |
new EFTracingConnection | |
{ | |
ConnectionString = wrapperConnectionString | |
}; | |
return connection; | |
} | |
// Please note that we can't override the method due to the change to its signature. | |
public int SaveChanges(string tableName) | |
{ | |
var objs = from e in this.ChangeTracker.Entries() | |
where e.State == EntityState.Added | |
select e; | |
foreach (var obj in objs) | |
{ | |
var ids = this.Database.SqlQuery<decimal>("select NET_VVS_TEST.ID_GENERATOR.GenerateId('" + tableName + "') as NEXTID from dual"); | |
var o = obj.Entity as IdentityObject; | |
foreach (decimal id in ids) | |
{ | |
if (o != null) | |
{ | |
o.Id = id; | |
break; | |
} | |
} | |
} | |
return base.SaveChanges(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment