public static string GetTableName <T>() where T : class { string name = ""; object[] attributes = typeof(T).GetCustomAttributes(true); foreach (object attribute in attributes) { string typeName = attribute.GetType().Name; if (typeName == "TableAttribute") //this is the [Table("Accounts")] at the top of the class, it tells what table to look at. { System.ComponentModel.DataAnnotations.Schema.TableAttribute ta = (System.ComponentModel.DataAnnotations.Schema.TableAttribute)attribute; name = ta.Name; } } //if no table attribute was found then create the table based on the name of the class. // if (string.IsNullOrEmpty(name)) { name = typeof(T).Name; //If it's namespaced then get the very last element in the namespace (class name). if (name.Contains(".")) { string[] tokens = name.Split('.'); name = tokens[tokens.Length - 1]; } } return(name); }
public static IEdmModel Metadata() { //Создает модель EDM (модель EDM). OData Endpoint ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.Namespace = poco.codeConfig.folderName; builder.ContainerName = "Default"; Assembly assembly = poco.assembly; var exportedTypes = assembly.GetExportedTypes(); foreach (Type typeClass in exportedTypes) { //Take from poco class a [TableAttribute] System.ComponentModel.DataAnnotations.Schema.TableAttribute tableAttribute = typeClass.GetCustomAttribute(typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute)) as System.ComponentModel.DataAnnotations.Schema.TableAttribute; //Ограничение EF https://stackoverflow.com/questions/18638741/the-type-company-model-user-and-the-type-company-core-model-user-both-have-t builder.AddClrObject(new AssemblyTableInfo { SqlTableOwner = tableAttribute.Schema, SqlTableName = tableAttribute.Name, AssemblyName = typeClass.Name, AssemblyFullName = typeClass.FullName, ClrTypeClass = typeClass, EntitySetName = typeClass.Name }); } EdmModel model = (EdmModel)builder.GetEdmModel(); return(model); }
private List <rptDBAuditTrail> GetAuditRecordsForChange(DbEntityEntry dbEntry, string UserId) { List <rptDBAuditTrail> result = new List <rptDBAuditTrail>(); try { DateTime changeTime = DateTime.Now; System.ComponentModel.DataAnnotations.Schema.TableAttribute tableAttr = dbEntry.Entity.GetType().GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.Schema.TableAttribute), false).FirstOrDefault() as System.ComponentModel.DataAnnotations.Schema.TableAttribute; string tableName = tableAttr != null ? tableAttr.Name : dbEntry.Entity.GetType().Name; var keyNames = dbEntry.Entity.GetType().GetProperties().Where(p => p.GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.KeyAttribute), false).Count() > 0).ToList(); string keyName = keyNames[0].Name; if (dbEntry.State == System.Data.Entity.EntityState.Deleted) { result.Add(new rptDBAuditTrail() { ItbId = Guid.NewGuid(), userid = UserId, eventdateutc = changeTime, eventtype = "D", // Deleted tablename = tableName, recordid = dbEntry.GetDatabaseValues().GetValue <object>(keyName).ToString(), columnname = "*ALL", newvalue = "yes", } ); } else if (dbEntry.State == System.Data.Entity.EntityState.Modified) { foreach (string propertyName in dbEntry.OriginalValues.PropertyNames) { var gf = dbEntry.GetDatabaseValues().GetValue <object>(propertyName) == null ? null : dbEntry.GetDatabaseValues().GetValue <object>(propertyName).ToString(); var ga = dbEntry.CurrentValues.GetValue <object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue <object>(propertyName).ToString(); if (gf != ga) { result.Add(new rptDBAuditTrail() { ItbId = Guid.NewGuid(), userid = UserId, eventdateutc = changeTime, eventtype = "M", // Modified tablename = tableName, recordid = dbEntry.OriginalValues.GetValue <object>(keyName).ToString(), columnname = propertyName, originalvalue = dbEntry.GetDatabaseValues().GetValue <object>(propertyName) == null ? null : dbEntry.GetDatabaseValues().GetValue <object>(propertyName).ToString(), newvalue = dbEntry.CurrentValues.GetValue <object>(propertyName) == null ? null : dbEntry.CurrentValues.GetValue <object>(propertyName).ToString() } ); } } } } catch { } return(result); }