public static IEnumerable GetColumnsNames() { var model = new AttributeMappingSource().GetModel(typeof(NorthwindDataContext)); return(from name in model.GetMetaType(typeof(Employee)).DataMembers select name.MappedName); }
public void Ctor_FileOrServerOrConnectionIsFilename() { MappingSource mapping = new AttributeMappingSource(); string fileOrServerOrConnection = typeof(DataContextTest).Assembly.Location; new DataContext(fileOrServerOrConnection, mapping); }
private Dictionary <MetaDataMember, Func <object, object> > GetGetterTableMap(Type tableType) { AttributeMappingSource mappingSource = new AttributeMappingSource(); MetaModel mapping = mappingSource.GetModel(tableType); MetaTable table = mapping.GetTable(tableType); Dictionary <MetaDataMember, Func <object, object> > mappedTable = new Dictionary <MetaDataMember, Func <object, object> >(); foreach (MetaDataMember dataMember in table.RowType.PersistentDataMembers) { MemberInfo memberInfo = dataMember.Member; Expression <Func <object, object> > getter = null; if (memberInfo is FieldInfo) { getter = (Expression <Func <object, object> >)(o => ((FieldInfo)memberInfo).GetValue(o)); } else if (memberInfo is PropertyInfo) { getter = (Expression <Func <object, object> >)(o => ((PropertyInfo)memberInfo).GetGetMethod().Invoke(o, new object[0])); } else { throw new ApplicationException("GetTableMap could not determine lambda expression for " + memberInfo.GetType() + "."); } mappedTable.Add(dataMember, getter.Compile()); } return(mappedTable); }
public void Ctor_FileOrServerOrConnectionIsServer() { MappingSource mapping = new AttributeMappingSource(); string fileOrServerOrConnection = "ThisIsAssumedToBeAServerName"; new DataContext(fileOrServerOrConnection, mapping); }
public void Ctor_FileOrServerOrConnectionIsNull() { MappingSource mapping = new AttributeMappingSource(); string fileOrServerOrConnection = null; new DataContext(fileOrServerOrConnection, mapping); }
private string GetTableName(Type tableType) { AttributeMappingSource mappingSource = new AttributeMappingSource(); MetaModel mapping = mappingSource.GetModel(tableType); MetaTable table = mapping.GetTable(tableType); return(table.TableName); }
void Initialize(string providerName, MetaModel mapping) { if (mapping == null) { Type thisType = GetType(); if (thisType != typeof(Database)) { mapping = new AttributeMappingSource().GetModel(thisType); } } this.config = new DatabaseConfiguration(mapping); DbProviderFactory factory = this.Connection.GetProviderFactory(); this.cb = factory.CreateCommandBuilder(); this.config.LastInsertIdCommand = "SELECT @@identity"; this.config.DeleteConflictPolicy = ConcurrencyConflictPolicy.IgnoreVersionAndLowerAffectedRecords; this.config.EnableBatchCommands = true; this.config.EnableInsertRecursion = true; if (providerName != null) { string identityKey = String.Format(CultureInfo.InvariantCulture, "DbExtensions:{0}:LastInsertIdCommand", providerName); string identitySetting = ConfigurationManager.AppSettings[identityKey]; if (identitySetting != null) { this.config.LastInsertIdCommand = identitySetting; } string batchKey = String.Format(CultureInfo.InvariantCulture, "DbExtensions:{0}:EnableBatchCommands", providerName); string batchSetting = ConfigurationManager.AppSettings[batchKey]; if (batchSetting != null) { bool batch; if (!Boolean.TryParse(batchSetting, out batch)) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, "The {0} appication setting must be a valid boolean.", batchSetting)); } this.config.EnableBatchCommands = batch; } } if (mapping != null) { if (mapping.ProviderType == typeof(System.Data.Linq.SqlClient.Sql2008Provider)) { this.config.SqlDialect = SqlDialect.SqlServer2008; } } }
public void ProviderAndDatabaseNameTest() { //Test Provider var mappingSource = new AttributeMappingSource(); var model = mappingSource.GetModel(typeof(Database1)); Assert.AreSame(typeof(AccessDbProvider), model.ProviderType); //Test DatabaseName Assert.AreEqual <string>("Northwind", model.DatabaseName); }
//public int TotalRecords { get; set; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { var model = new AttributeMappingSource().GetModel(typeof(DataClassesDataContext)); using (Broker.LoggingDataAccess.LoggingDataContext dc = new LoggingDataContext()) { string ColName = ""; List <Broker.LoggingDataAccess.DBTranslatedColumnName> columnList = new List <Broker.LoggingDataAccess.DBTranslatedColumnName>(); foreach (var mt in model.GetTables()) { foreach (var dm in mt.RowType.PersistentDataMembers) { if (dm.DbType != null) { ColName = dm.MappedName; //bool contains = false; //foreach (Broker.LoggingDataAccess.DBTranslatedColumnName item in columnList) //{ // if (item.Name == ColName) // { // contains = true; // } //} //if (!contains) //{ if (dc.DBTranslatedColumnNames.Where(c => c.Name == ColName).FirstOrDefault() == null) { Broker.LoggingDataAccess.DBTranslatedColumnName newColumn = new Broker.LoggingDataAccess.DBTranslatedColumnName(); newColumn.Name = ColName; newColumn.NameMKD = string.Empty; dc.DBTranslatedColumnNames.InsertOnSubmit(newColumn); dc.SubmitChanges(); } } } } //TotalRecords = dc.DBTranslatedColumnNames.Count(); gvTables.DataSource = dc.DBTranslatedColumnNames; gvTables.DataBind(); } } }
public void TableTest() { var mappingSource = new AttributeMappingSource(); var model = mappingSource.GetModel(typeof(Database2)); var metaTables = model.GetTables().ToArray(); //Test meta tables Count Assert.AreEqual(2, metaTables.Count()); //Test meta table name. Assert.AreEqual("Table1", metaTables[0].TableName); Assert.AreEqual("Table2", metaTables[1].TableName); }
/// <summary> /// Método Genérico para Atualizar um Registro /// </summary> /// <typeparam name="T">Tipagem da Classe</typeparam> /// <param name="entidade">Objeto a ser Atualizado no Banco</param> /// <param name="dataContext">Conntexto</param> public void Atualizar(T entidade, DataContext dataContext) { try { using (dataContext) { Table <T> tabela = dataContext.GetTable <T>(); T original = tabela.FirstOrDefault(e => e == entidade); if (original != null) { var model = new AttributeMappingSource().GetModel(dataContext.GetType()); MetaTable tbl = model.GetTable(typeof(T)); foreach (var dm in tbl.RowType.DataMembers) { PropertyInfo p1 = original.GetType().GetProperty(dm.MappedName); PropertyInfo p2 = entidade.GetType().GetProperty(dm.MappedName); try { if (p1.PropertyType.ToString() != "System.DateTime") { p1.SetValue(original, p2.GetValue(entidade, null), null); } else { string dtStr = p2.GetValue(entidade, null).ToString(); DateTime dt = Convert.ToDateTime(dtStr); System.Diagnostics.Trace.WriteLine(dt.Year); if (dt.Year > 1) { p1.SetValue(original, p2.GetValue(entidade, null), null); } } } catch { System.Diagnostics.Trace.WriteLine(dm.MappedName); } } } dataContext.SubmitChanges(); } } catch (Exception) { throw; } }
private Type GetMemberType(MemberExpression m) { AttributeMappingSource mappingSource = new AttributeMappingSource(); MetaModel mapping = mappingSource.GetModel(m.Member.DeclaringType); MetaTable table = mapping.GetTable(m.Member.DeclaringType); foreach (MetaDataMember dataMember in table.RowType.PersistentDataMembers) { if (dataMember.Name == m.Member.Name) { return(dataMember.Type); } } return(null); }
internal static bool DeleteWithCascase <T>(System.Data.Linq.DataContext ctx, T entity) where T : class, INotifyPropertyChanged { //Get the primary key of the context var pkey = GetPrimaryKey <T>(); //Get foreign keys var fkeys = GetForeignKeys <T>(); //get the model for the ctx var model = new AttributeMappingSource().GetModel(ctx.GetType()); //Get the table for the given entity var eTable = ctx.GetTable <T>(); foreach (var modelTable in model.GetTables()) { //Loop tables and delete } return(true); }
protected void Page_Load(object sender, EventArgs e) { if (!(IsPostBack)) { var model = new AttributeMappingSource().GetModel(typeof(DataClassesDataContext)); List <TableDetail> tableDetails = new List <TableDetail>(); List <MetaTable> temp = model.GetTables().ToList(); for (int i = 0; i < temp.Count; i++) { string tableName = temp[i].TableName; string prefix = tableName.Substring(0, 3); string tableWithoutPrefix = tableName.Substring(4); string tableNameInDB = "[" + prefix + "].[" + tableWithoutPrefix + "]"; using (Broker.LoggingDataAccess.LoggingDataContext dc = new LoggingDataContext()) { TableDetail newDetail = new TableDetail(); newDetail.Name = tableWithoutPrefix; newDetail.Index = i; newDetail.Insert = true; newDetail.Delete = true; newDetail.Select = true; newDetail.Update = true; DBLoggingIgnoredTable tempIgnoredTable = dc.DBLoggingIgnoredTables.Where(c => c.TableName == tableWithoutPrefix).FirstOrDefault(); if (tempIgnoredTable != null) { int ignoredTableID = tempIgnoredTable.ID; foreach (DBLoggingIgnoredTableActivity item in dc.DBLoggingIgnoredTableActivities.Where(c => c.DbLoggingIgnoredTableID == ignoredTableID).ToList()) { switch (item.ActivityTypeID) { case 1: { newDetail.Insert = false; break; } case 2: { newDetail.Delete = false; break; } case 3: { newDetail.Select = false; break; } case 4: { newDetail.Update = false; break; } default: break; } } } tableDetails.Add(newDetail); } } gvTables.DataSource = tableDetails; gvTables.DataBind(); } }