private void ReadQueryPanels(EntityMeta em, AggtBlocks result) { //导航实体的关系对象 var queryTypes = em.EntityType.GetCustomAttributes(typeof(ConditionQueryTypeAttribute), false); foreach (ConditionQueryTypeAttribute conAttri in queryTypes) { var surBlock = new ConditionBlock { EntityType = conAttri.QueryType, }; var surEM = CommonModel.Entities.Get(surBlock.EntityType); var surAggt = this.ReadCode(surEM, surBlock); result.Surrounders.Add(surAggt); } queryTypes = em.EntityType.GetCustomAttributes(typeof(NavigationQueryTypeAttribute), false); foreach (NavigationQueryTypeAttribute naviAttri in queryTypes) { var surBlock = new NavigationBlock { EntityType = naviAttri.QueryType, }; var surEM = CommonModel.Entities.Get(surBlock.EntityType); var surAggt = this.ReadCode(surEM, surBlock); result.Surrounders.Add(surAggt); } }
protected AggtBlocks ReadCode(EntityMeta em, Block mainBlock, ReadCodeTemplateOptions op = null) { op = op ?? new ReadCodeTemplateOptions { ReadChildren = true, ReadQueryPanels = true }; AggtBlocks result = mainBlock; if (op.ReadChildren) { foreach (var property in em.ChildrenProperties) { var childBlock = new ChildBlock(property.ManagedProperty as IListProperty); var childAggt = this.ReadCode(property.ChildType, childBlock); result.Children.Add(childAggt); } } if (op.ReadQueryPanels) { this.ReadQueryPanels(em, result); } return result; }
static CurrencyRate() { // Cache field metadata. EntityMeta <CurrencyRate> .LoadColumnMetadata ( new ColumnMetadata(nameof(CurrencyRateID), @"CurrencyRateID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(CurrencyRateDate), @"CurrencyRateDate", DbType.DateTime, isRequired : true), new ColumnMetadata(nameof(FromCurrencyCode), @"FromCurrencyCode", DbType.StringFixedLength, isRequired : true), new ColumnMetadata(nameof(ToCurrencyCode), @"ToCurrencyCode", DbType.StringFixedLength, isRequired : true), new ColumnMetadata(nameof(AverageRate), @"AverageRate", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(EndOfDayRate), @"EndOfDayRate", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <CurrencyRate> .LoadRelationMetadata ( new RelationMetadata(nameof(e_FromCurrencyCode), nameof(FromCurrencyCode), nameof(AdventureWorks.Sales.Currency.CurrencyCode)), new RelationMetadata(nameof(e_ToCurrencyCode), nameof(ToCurrencyCode), nameof(AdventureWorks.Sales.Currency.CurrencyCode)) ); // Cache table metadata. EntityMeta <CurrencyRate> .HasLanguageTable = false; EntityMeta <CurrencyRate> .TableSchema = @"Sales"; EntityMeta <CurrencyRate> .TableName = @"CurrencyRate"; EntityMeta <CurrencyRate> .LanguageTableSchema = null; EntityMeta <CurrencyRate> .LanguageTableName = null; }
static Products() { // Cache field metadata. EntityMeta <Products> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(ProductName), @"ProductName", DbType.String, isRequired : true), new ColumnMetadata(nameof(SupplierID), @"SupplierID", DbType.Int32, isRequired : false), new ColumnMetadata(nameof(CategoryID), @"CategoryID", DbType.Int32, isRequired : false), new ColumnMetadata(nameof(QuantityPerUnit), @"QuantityPerUnit", DbType.String, isRequired : false), new ColumnMetadata(nameof(UnitPrice), @"UnitPrice", DbType.Decimal, isRequired : false), new ColumnMetadata(nameof(UnitsInStock), @"UnitsInStock", DbType.Int16, isRequired : false), new ColumnMetadata(nameof(UnitsOnOrder), @"UnitsOnOrder", DbType.Int16, isRequired : false), new ColumnMetadata(nameof(ReorderLevel), @"ReorderLevel", DbType.Int16, isRequired : false), new ColumnMetadata(nameof(Discontinued), @"Discontinued", DbType.Boolean, isRequired : true) ); // Cache entites metadata. EntityMeta <Products> .LoadRelationMetadata ( new RelationMetadata(nameof(Categories), nameof(CategoryID), nameof(Northwind.Dbo.Categories.CategoryID)), new RelationMetadata(nameof(Suppliers), nameof(SupplierID), nameof(Northwind.Dbo.Suppliers.SupplierID)) ); // Cache table metadata. EntityMeta <Products> .HasLanguageTable = false; EntityMeta <Products> .TableSchema = @"dbo"; EntityMeta <Products> .TableName = @"Products"; EntityMeta <Products> .LanguageTableSchema = null; EntityMeta <Products> .LanguageTableName = null; }
public string BuildReplaceInsertSQL(EntityMeta meta) { if (meta.Columns == null || meta.Columns.Count == 0) { return(string.Empty); } var keys = meta.Columns.FindAll(_ => _.PrimaryKey); AssertUtils.IsTrue(keys.Count > 0, $"表{meta.TableName} 不存在主键"); StringBuilder sqlbuilder = new StringBuilder(); sqlbuilder.AppendFormat(" IF NOT EXISTS (SELECT 1 FROM [{0}] WHERE ", meta.TableName); for (int i = 0; i < keys.Count; i++) { if (i > 0) { sqlbuilder.Append(" AND "); } sqlbuilder.Append("[" + keys[i].ColumnName + "]=@" + keys[i].PropertyName); } sqlbuilder.Append(" )"); sqlbuilder.AppendFormat(" BEGIN {0} END", BuildInsertSql(meta)); sqlbuilder.Append(" ELSE"); sqlbuilder.AppendFormat(" BEGIN {0} END", BuildUpdateSql(meta)); return(sqlbuilder.ToString()); }
static ProductVendor() { // Cache field metadata. EntityMeta <ProductVendor> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(BusinessEntityID), @"BusinessEntityID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(AverageLeadTime), @"AverageLeadTime", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(StandardPrice), @"StandardPrice", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(LastReceiptCost), @"LastReceiptCost", DbType.Decimal, isRequired : false), new ColumnMetadata(nameof(LastReceiptDate), @"LastReceiptDate", DbType.DateTime, isRequired : false), new ColumnMetadata(nameof(MinOrderQty), @"MinOrderQty", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(MaxOrderQty), @"MaxOrderQty", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(OnOrderQty), @"OnOrderQty", DbType.Int32, isRequired : false), new ColumnMetadata(nameof(UnitMeasureCode), @"UnitMeasureCode", DbType.StringFixedLength, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ProductVendor> .LoadRelationMetadata ( new RelationMetadata(nameof(e_BusinessEntityID), nameof(BusinessEntityID), nameof(AdventureWorks.Purchasing.Vendor.BusinessEntityID)), new RelationMetadata(nameof(e_ProductID), nameof(ProductID), nameof(AdventureWorks.Production.Product.ProductID)), new RelationMetadata(nameof(e_UnitMeasureCode), nameof(UnitMeasureCode), nameof(AdventureWorks.Production.UnitMeasure.UnitMeasureCode)) ); // Cache table metadata. EntityMeta <ProductVendor> .HasLanguageTable = false; EntityMeta <ProductVendor> .TableSchema = @"Purchasing"; EntityMeta <ProductVendor> .TableName = @"ProductVendor"; EntityMeta <ProductVendor> .LanguageTableSchema = null; EntityMeta <ProductVendor> .LanguageTableName = null; }
static SalesTerritory() { // Cache field metadata. EntityMeta <SalesTerritory> .LoadColumnMetadata ( new ColumnMetadata(nameof(TerritoryID), @"TerritoryID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(Name), @"Name", DbType.String, isRequired : true), new ColumnMetadata(nameof(CountryRegionCode), @"CountryRegionCode", DbType.String, isRequired : true), new ColumnMetadata(nameof(Group), @"Group", DbType.String, isRequired : true), new ColumnMetadata(nameof(SalesYTD), @"SalesYTD", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(SalesLastYear), @"SalesLastYear", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(CostYTD), @"CostYTD", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(CostLastYear), @"CostLastYear", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <SalesTerritory> .LoadRelationMetadata ( new RelationMetadata(nameof(e_CountryRegionCode), nameof(CountryRegionCode), nameof(AdventureWorks.Person.CountryRegion.CountryRegionCode)) ); // Cache table metadata. EntityMeta <SalesTerritory> .HasLanguageTable = false; EntityMeta <SalesTerritory> .TableSchema = @"Sales"; EntityMeta <SalesTerritory> .TableName = @"SalesTerritory"; EntityMeta <SalesTerritory> .LanguageTableSchema = null; EntityMeta <SalesTerritory> .LanguageTableName = null; }
static ShipMethod() { // Cache field metadata. EntityMeta <ShipMethod> .LoadColumnMetadata ( new ColumnMetadata(nameof(ShipMethodID), @"ShipMethodID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(Name), @"Name", DbType.String, isRequired : true), new ColumnMetadata(nameof(ShipBase), @"ShipBase", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(ShipRate), @"ShipRate", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ShipMethod> .LoadRelationMetadata ( null ); // Cache table metadata. EntityMeta <ShipMethod> .HasLanguageTable = false; EntityMeta <ShipMethod> .TableSchema = @"Purchasing"; EntityMeta <ShipMethod> .TableName = @"ShipMethod"; EntityMeta <ShipMethod> .LanguageTableSchema = null; EntityMeta <ShipMethod> .LanguageTableName = null; }
static SalesTerritoryHistory() { // Cache field metadata. EntityMeta <SalesTerritoryHistory> .LoadColumnMetadata ( new ColumnMetadata(nameof(BusinessEntityID), @"BusinessEntityID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(TerritoryID), @"TerritoryID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(StartDate), @"StartDate", DbType.DateTime, isPrimaryKey : true), new ColumnMetadata(nameof(EndDate), @"EndDate", DbType.DateTime, isRequired : false), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <SalesTerritoryHistory> .LoadRelationMetadata ( new RelationMetadata(nameof(e_BusinessEntityID), nameof(BusinessEntityID), nameof(AdventureWorks.Sales.SalesPerson.BusinessEntityID)), new RelationMetadata(nameof(e_TerritoryID), nameof(TerritoryID), nameof(AdventureWorks.Sales.SalesTerritory.TerritoryID)) ); // Cache table metadata. EntityMeta <SalesTerritoryHistory> .HasLanguageTable = false; EntityMeta <SalesTerritoryHistory> .TableSchema = @"Sales"; EntityMeta <SalesTerritoryHistory> .TableName = @"SalesTerritoryHistory"; EntityMeta <SalesTerritoryHistory> .LanguageTableSchema = null; EntityMeta <SalesTerritoryHistory> .LanguageTableName = null; }
static ProductSubcategory() { // Cache field metadata. EntityMeta <ProductSubcategory> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductSubcategoryID), @"ProductSubcategoryID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(ProductCategoryID), @"ProductCategoryID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(Name), @"Name", DbType.String, isRequired : true), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ProductSubcategory> .LoadRelationMetadata ( new RelationMetadata(nameof(e_ProductCategoryID), nameof(ProductCategoryID), nameof(AdventureWorks.Production.ProductCategory.ProductCategoryID)) ); // Cache table metadata. EntityMeta <ProductSubcategory> .HasLanguageTable = false; EntityMeta <ProductSubcategory> .TableSchema = @"Production"; EntityMeta <ProductSubcategory> .TableName = @"ProductSubcategory"; EntityMeta <ProductSubcategory> .LanguageTableSchema = null; EntityMeta <ProductSubcategory> .LanguageTableName = null; }
static SalesOrderDetail() { // Cache field metadata. EntityMeta <SalesOrderDetail> .LoadColumnMetadata ( new ColumnMetadata(nameof(SalesOrderID), @"SalesOrderID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(SalesOrderDetailID), @"SalesOrderDetailID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(CarrierTrackingNumber), @"CarrierTrackingNumber", DbType.String, isRequired : false), new ColumnMetadata(nameof(OrderQty), @"OrderQty", DbType.Int16, isRequired : true), new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(SpecialOfferID), @"SpecialOfferID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(UnitPrice), @"UnitPrice", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(UnitPriceDiscount), @"UnitPriceDiscount", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(LineTotal), @"LineTotal", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <SalesOrderDetail> .LoadRelationMetadata ( new RelationMetadata(nameof(e_SalesOrderID), nameof(SalesOrderID), nameof(AdventureWorks.Sales.SalesOrderHeader.SalesOrderID)), new RelationMetadata(nameof(SpecialOfferIDProductID), nameof(SpecialOfferID), nameof(AdventureWorks.Sales.SpecialOfferProduct.SpecialOfferID)), new RelationMetadata(nameof(SpecialOfferIDProductID), nameof(ProductID), nameof(AdventureWorks.Sales.SpecialOfferProduct.ProductID)) ); // Cache table metadata. EntityMeta <SalesOrderDetail> .HasLanguageTable = false; EntityMeta <SalesOrderDetail> .TableSchema = @"Sales"; EntityMeta <SalesOrderDetail> .TableName = @"SalesOrderDetail"; EntityMeta <SalesOrderDetail> .LanguageTableSchema = null; EntityMeta <SalesOrderDetail> .LanguageTableName = null; }
public string BuildSelectByPkSql(EntityMeta meta) { if (meta.Columns == null || meta.Columns.Count == 0) { return(string.Empty); } List <string> selectColumns = new List <string>(); meta.Columns.ForEach(item => selectColumns.Add(string.Format("[{0}]", item.ColumnName))); StringBuilder sqlbuilder = new StringBuilder(); string selectColumnStr = string.Join(",", selectColumns); sqlbuilder.AppendFormat("SELECT {0} from [{1}] WHERE", selectColumnStr, meta.TableName); var pkList = meta.Columns.FindAll(item => item.PrimaryKey); AssertUtils.IsTrue(pkList.Count > 0, $"表{meta.TableName} 不存在主键"); if (pkList != null && pkList.Count > 0) { sqlbuilder.Append(" 1=1 "); foreach (var item in pkList) { sqlbuilder.AppendFormat(" AND [{0}]=@{1}", item.ColumnName, item.PropertyName); } } return(sqlbuilder.ToString()); }
static BusinessEntityAddress() { // Cache field metadata. EntityMeta <BusinessEntityAddress> .LoadColumnMetadata ( new ColumnMetadata(nameof(BusinessEntityID), @"BusinessEntityID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(AddressID), @"AddressID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(AddressTypeID), @"AddressTypeID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <BusinessEntityAddress> .LoadRelationMetadata ( new RelationMetadata(nameof(e_AddressID), nameof(AddressID), nameof(AdventureWorks.Person.Address.AddressID)), new RelationMetadata(nameof(e_AddressTypeID), nameof(AddressTypeID), nameof(AdventureWorks.Person.AddressType.AddressTypeID)), new RelationMetadata(nameof(e_BusinessEntityID), nameof(BusinessEntityID), nameof(AdventureWorks.Person.BusinessEntity.BusinessEntityID)) ); // Cache table metadata. EntityMeta <BusinessEntityAddress> .HasLanguageTable = false; EntityMeta <BusinessEntityAddress> .TableSchema = @"Person"; EntityMeta <BusinessEntityAddress> .TableName = @"BusinessEntityAddress"; EntityMeta <BusinessEntityAddress> .LanguageTableSchema = null; EntityMeta <BusinessEntityAddress> .LanguageTableName = null; }
static CreditCard() { // Cache field metadata. EntityMeta <CreditCard> .LoadColumnMetadata ( new ColumnMetadata(nameof(CreditCardID), @"CreditCardID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(CardType), @"CardType", DbType.String, isRequired : true), new ColumnMetadata(nameof(CardNumber), @"CardNumber", DbType.String, isRequired : true), new ColumnMetadata(nameof(ExpMonth), @"ExpMonth", DbType.Byte, isRequired : true), new ColumnMetadata(nameof(ExpYear), @"ExpYear", DbType.Int16, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <CreditCard> .LoadRelationMetadata ( null ); // Cache table metadata. EntityMeta <CreditCard> .HasLanguageTable = false; EntityMeta <CreditCard> .TableSchema = @"Sales"; EntityMeta <CreditCard> .TableName = @"CreditCard"; EntityMeta <CreditCard> .LanguageTableSchema = null; EntityMeta <CreditCard> .LanguageTableName = null; }
internal DbTable(IRepositoryInternal repository) { _repository = repository; _meta = repository.EntityMeta; _name = _meta.TableMeta.TableName; _columns = new List <DbColumn>(); }
protected AggtBlocks ReadCode(EntityMeta em, Block mainBlock, ReadCodeTemplateOptions op = null) { op = op ?? new ReadCodeTemplateOptions { ReadChildren = true, ReadQueryPanels = true }; AggtBlocks result = mainBlock; if (op.ReadChildren) { foreach (var property in em.ChildrenProperties) { var childBlock = new ChildBlock(property.ManagedProperty as IListProperty); var childAggt = this.ReadCode(property.ChildType, childBlock); result.Children.Add(childAggt); } } if (op.ReadQueryPanels) { this.ReadQueryPanels(em, result); } return(result); }
static WorkOrderRouting() { // Cache field metadata. EntityMeta <WorkOrderRouting> .LoadColumnMetadata ( new ColumnMetadata(nameof(WorkOrderID), @"WorkOrderID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(OperationSequence), @"OperationSequence", DbType.Int16, isPrimaryKey : true), new ColumnMetadata(nameof(LocationID), @"LocationID", DbType.Int16, isRequired : true), new ColumnMetadata(nameof(ScheduledStartDate), @"ScheduledStartDate", DbType.DateTime, isRequired : true), new ColumnMetadata(nameof(ScheduledEndDate), @"ScheduledEndDate", DbType.DateTime, isRequired : true), new ColumnMetadata(nameof(ActualStartDate), @"ActualStartDate", DbType.DateTime, isRequired : false), new ColumnMetadata(nameof(ActualEndDate), @"ActualEndDate", DbType.DateTime, isRequired : false), new ColumnMetadata(nameof(ActualResourceHrs), @"ActualResourceHrs", DbType.Decimal, isRequired : false), new ColumnMetadata(nameof(PlannedCost), @"PlannedCost", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(ActualCost), @"ActualCost", DbType.Decimal, isRequired : false), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <WorkOrderRouting> .LoadRelationMetadata ( new RelationMetadata(nameof(e_LocationID), nameof(LocationID), nameof(AdventureWorks.Production.Location.LocationID)), new RelationMetadata(nameof(e_WorkOrderID), nameof(WorkOrderID), nameof(AdventureWorks.Production.WorkOrder.WorkOrderID)) ); // Cache table metadata. EntityMeta <WorkOrderRouting> .HasLanguageTable = false; EntityMeta <WorkOrderRouting> .TableSchema = @"Production"; EntityMeta <WorkOrderRouting> .TableName = @"WorkOrderRouting"; EntityMeta <WorkOrderRouting> .LanguageTableSchema = null; EntityMeta <WorkOrderRouting> .LanguageTableName = null; }
internal RdbTable(IRepositoryInternal repository) { _repository = repository; _meta = repository.EntityMeta; _tableInfo = repository.TableInfo; _columns = new List<RdbColumn>(); }
/// <summary> /// 删除不必要的对象,只留下需要保存的“脏”数据 /// </summary> /// <param name="diffEntity">The difference entity.</param> /// <param name="entityInfo">The entity information.</param> protected virtual void ClearDataCore(Entity diffEntity, EntityMeta entityInfo) { foreach (var item in entityInfo.ChildrenProperties) { var mp = item.ManagedProperty; //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值 if (!diffEntity.FieldExists(mp)) { continue; } var children = diffEntity.GetProperty(mp) as EntityList; if (children == null) { continue; } for (int i = children.Count - 1; i >= 0; i--) { var child = children[i]; if (!child.IsDirty) { children.Remove(child); children.DeletedList.Remove(child); } else { this.ClearData(child); } } } }
static ProductModel() { // Cache field metadata. EntityMeta <ProductModel> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductModelID), @"ProductModelID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(Name), @"Name", DbType.String, isRequired : true), new ColumnMetadata(nameof(CatalogDescription), @"CatalogDescription", DbType.Xml, isRequired : false), new ColumnMetadata(nameof(Instructions), @"Instructions", DbType.Xml, isRequired : false), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ProductModel> .LoadRelationMetadata ( null ); // Cache table metadata. EntityMeta <ProductModel> .HasLanguageTable = false; EntityMeta <ProductModel> .TableSchema = @"Production"; EntityMeta <ProductModel> .TableName = @"ProductModel"; EntityMeta <ProductModel> .LanguageTableSchema = null; EntityMeta <ProductModel> .LanguageTableName = null; }
static TransactionHistory() { // Cache field metadata. EntityMeta <TransactionHistory> .LoadColumnMetadata ( new ColumnMetadata(nameof(TransactionID), @"TransactionID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(ReferenceOrderID), @"ReferenceOrderID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(ReferenceOrderLineID), @"ReferenceOrderLineID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(TransactionDate), @"TransactionDate", DbType.DateTime, isRequired : true), new ColumnMetadata(nameof(TransactionType), @"TransactionType", DbType.StringFixedLength, isRequired : true), new ColumnMetadata(nameof(Quantity), @"Quantity", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(ActualCost), @"ActualCost", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <TransactionHistory> .LoadRelationMetadata ( new RelationMetadata(nameof(e_ProductID), nameof(ProductID), nameof(AdventureWorks.Production.Product.ProductID)) ); // Cache table metadata. EntityMeta <TransactionHistory> .HasLanguageTable = false; EntityMeta <TransactionHistory> .TableSchema = @"Production"; EntityMeta <TransactionHistory> .TableName = @"TransactionHistory"; EntityMeta <TransactionHistory> .LanguageTableSchema = null; EntityMeta <TransactionHistory> .LanguageTableName = null; }
static Shift() { // Cache field metadata. EntityMeta <Shift> .LoadColumnMetadata ( new ColumnMetadata(nameof(ShiftID), @"ShiftID", DbType.Byte, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(Name), @"Name", DbType.String, isRequired : true), new ColumnMetadata(nameof(StartTime), @"StartTime", DbType.Time, isRequired : true), new ColumnMetadata(nameof(EndTime), @"EndTime", DbType.Time, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <Shift> .LoadRelationMetadata ( null ); // Cache table metadata. EntityMeta <Shift> .HasLanguageTable = false; EntityMeta <Shift> .TableSchema = @"HumanResources"; EntityMeta <Shift> .TableName = @"Shift"; EntityMeta <Shift> .LanguageTableSchema = null; EntityMeta <Shift> .LanguageTableName = null; }
static Person() { // Cache field metadata. EntityMeta <Person> .LoadColumnMetadata ( new ColumnMetadata(nameof(BusinessEntityID), @"BusinessEntityID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(PersonType), @"PersonType", DbType.StringFixedLength, isRequired : true), new ColumnMetadata(nameof(NameStyle), @"NameStyle", DbType.Boolean, isRequired : true), new ColumnMetadata(nameof(Title), @"Title", DbType.String, isRequired : false), new ColumnMetadata(nameof(FirstName), @"FirstName", DbType.String, isRequired : true), new ColumnMetadata(nameof(MiddleName), @"MiddleName", DbType.String, isRequired : false), new ColumnMetadata(nameof(LastName), @"LastName", DbType.String, isRequired : true), new ColumnMetadata(nameof(Suffix), @"Suffix", DbType.String, isRequired : false), new ColumnMetadata(nameof(EmailPromotion), @"EmailPromotion", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(AdditionalContactInfo), @"AdditionalContactInfo", DbType.Xml, isRequired : false), new ColumnMetadata(nameof(Demographics), @"Demographics", DbType.Xml, isRequired : false), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <Person> .LoadRelationMetadata ( new RelationMetadata(nameof(e_BusinessEntityID), nameof(BusinessEntityID), nameof(AdventureWorks.Person.BusinessEntity.BusinessEntityID)) ); // Cache table metadata. EntityMeta <Person> .HasLanguageTable = false; EntityMeta <Person> .TableSchema = @"Person"; EntityMeta <Person> .TableName = @"Person"; EntityMeta <Person> .LanguageTableSchema = null; EntityMeta <Person> .LanguageTableName = null; }
public void OpenStorage(bool initOrOpen) { AssertNotDisposed(); if (!File.Exists(_tagFile)) { if (!initOrOpen) { throw new Exception("Vault root path does not contain vault data"); } InitStorage(false); } var tagVers = File.ReadAllText(_tagFile); if (TAG_VERS != tagVers) { throw new Exception("Vault version mismatch"); } if (File.Exists(_vaultFile)) { using (var s = new FileStream(_vaultFile, FileMode.Open)) { _vaultMeta = JsonHelper.Load <EntityMeta <VaultConfig> >(s); } } IsOpen = true; }
static ProductCostHistory() { // Cache field metadata. EntityMeta <ProductCostHistory> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(StartDate), @"StartDate", DbType.DateTime, isPrimaryKey : true), new ColumnMetadata(nameof(EndDate), @"EndDate", DbType.DateTime, isRequired : false), new ColumnMetadata(nameof(StandardCost), @"StandardCost", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ProductCostHistory> .LoadRelationMetadata ( new RelationMetadata(nameof(e_ProductID), nameof(ProductID), nameof(AdventureWorks.Production.Product.ProductID)) ); // Cache table metadata. EntityMeta <ProductCostHistory> .HasLanguageTable = false; EntityMeta <ProductCostHistory> .TableSchema = @"Production"; EntityMeta <ProductCostHistory> .TableName = @"ProductCostHistory"; EntityMeta <ProductCostHistory> .LanguageTableSchema = null; EntityMeta <ProductCostHistory> .LanguageTableName = null; }
/// <summary> /// 清除子对象集合 /// </summary> /// <param name="oldEntity">The old entity.</param> /// <param name="entityInfo">The entity information.</param> protected virtual void MakeSavedCore(Entity oldEntity, EntityMeta entityInfo) { foreach (var item in entityInfo.ChildrenProperties) { var mp = item.ManagedProperty as IListProperty; //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值 if (!oldEntity.FieldExists(mp)) { continue; } var children = oldEntity.GetProperty(mp) as EntityList; if (children == null) { continue; } //清除已删除数据 children.CastTo <EntityList>().DeletedList.Clear(); //所有子对象,都标记为已保存 for (int i = children.Count - 1; i >= 0; i--) { var child = children[i] as Entity; if (child.IsDirty || child.IsNew) { MakeSaved(child); } } } oldEntity.MarkSaved(); }
private static void ProcessManagedProperties(Type type, DbTable table, EntityMeta em) { foreach (var property in em.EntityProperties) { var meta = property.ColumnMeta; if (meta == null) { continue; } var propertyName = property.Name; //生成 ManagedPropertyBridge var epm = em.Property(propertyName); if (epm == null) { throw new ArgumentNullException(string.Format("{0}.{1} 属性需要使用托管属性进行编写。", type.FullName, propertyName)); } var mp = epm.ManagedProperty as IProperty; //列名 var columnName = meta.ColumnName; if (string.IsNullOrWhiteSpace(columnName)) { columnName = propertyName; } var column = table.CreateColumn(columnName, epm); column.IsIdentity = meta.IsIdentity; column.IsPrimaryKey = meta.IsPrimaryKey; table.Add(column); } }
internal RdbTable(IRepositoryInternal repository) { _repository = repository; _meta = repository.EntityMeta; _tableInfo = repository.TableInfo; _columns = new List <RdbColumn>(); }
static PurchaseOrderHeader() { // Cache field metadata. EntityMeta <PurchaseOrderHeader> .LoadColumnMetadata ( new ColumnMetadata(nameof(PurchaseOrderID), @"PurchaseOrderID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(RevisionNumber), @"RevisionNumber", DbType.Byte, isRequired : true), new ColumnMetadata(nameof(Status), @"Status", DbType.Byte, isRequired : true), new ColumnMetadata(nameof(EmployeeID), @"EmployeeID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(VendorID), @"VendorID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(ShipMethodID), @"ShipMethodID", DbType.Int32, isRequired : true), new ColumnMetadata(nameof(OrderDate), @"OrderDate", DbType.DateTime, isRequired : true), new ColumnMetadata(nameof(ShipDate), @"ShipDate", DbType.DateTime, isRequired : false), new ColumnMetadata(nameof(SubTotal), @"SubTotal", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(TaxAmt), @"TaxAmt", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(Freight), @"Freight", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(TotalDue), @"TotalDue", DbType.Decimal, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <PurchaseOrderHeader> .LoadRelationMetadata ( new RelationMetadata(nameof(e_EmployeeID), nameof(EmployeeID), nameof(AdventureWorks.HumanResources.Employee.BusinessEntityID)), new RelationMetadata(nameof(e_ShipMethodID), nameof(ShipMethodID), nameof(AdventureWorks.Purchasing.ShipMethod.ShipMethodID)), new RelationMetadata(nameof(e_VendorID), nameof(VendorID), nameof(AdventureWorks.Purchasing.Vendor.BusinessEntityID)) ); // Cache table metadata. EntityMeta <PurchaseOrderHeader> .HasLanguageTable = false; EntityMeta <PurchaseOrderHeader> .TableSchema = @"Purchasing"; EntityMeta <PurchaseOrderHeader> .TableName = @"PurchaseOrderHeader"; EntityMeta <PurchaseOrderHeader> .LanguageTableSchema = null; EntityMeta <PurchaseOrderHeader> .LanguageTableName = null; }
static Password() { // Cache field metadata. EntityMeta <Password> .LoadColumnMetadata ( new ColumnMetadata(nameof(BusinessEntityID), @"BusinessEntityID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(PasswordHash), @"PasswordHash", DbType.String, isRequired : true), new ColumnMetadata(nameof(PasswordSalt), @"PasswordSalt", DbType.String, isRequired : true), new ColumnMetadata(nameof(rowguid), @"rowguid", DbType.Guid, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <Password> .LoadRelationMetadata ( new RelationMetadata(nameof(e_BusinessEntityID), nameof(BusinessEntityID), nameof(AdventureWorks.Person.Person.BusinessEntityID)) ); // Cache table metadata. EntityMeta <Password> .HasLanguageTable = false; EntityMeta <Password> .TableSchema = @"Person"; EntityMeta <Password> .TableName = @"Password"; EntityMeta <Password> .LanguageTableSchema = null; EntityMeta <Password> .LanguageTableName = null; }
static ProductProductPhoto() { // Cache field metadata. EntityMeta <ProductProductPhoto> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductID), @"ProductID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(ProductPhotoID), @"ProductPhotoID", DbType.Int32, isPrimaryKey : true), new ColumnMetadata(nameof(Primary), @"Primary", DbType.Boolean, isRequired : true), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ProductProductPhoto> .LoadRelationMetadata ( new RelationMetadata(nameof(e_ProductID), nameof(ProductID), nameof(AdventureWorks.Production.Product.ProductID)), new RelationMetadata(nameof(e_ProductPhotoID), nameof(ProductPhotoID), nameof(AdventureWorks.Production.ProductPhoto.ProductPhotoID)) ); // Cache table metadata. EntityMeta <ProductProductPhoto> .HasLanguageTable = false; EntityMeta <ProductProductPhoto> .TableSchema = @"Production"; EntityMeta <ProductProductPhoto> .TableName = @"ProductProductPhoto"; EntityMeta <ProductProductPhoto> .LanguageTableSchema = null; EntityMeta <ProductProductPhoto> .LanguageTableName = null; }
static ProductPhoto() { // Cache field metadata. EntityMeta <ProductPhoto> .LoadColumnMetadata ( new ColumnMetadata(nameof(ProductPhotoID), @"ProductPhotoID", DbType.Int32, isIdentity : true, isPrimaryKey : true), new ColumnMetadata(nameof(ThumbNailPhoto), @"ThumbNailPhoto", DbType.Binary, isRequired : false), new ColumnMetadata(nameof(ThumbnailPhotoFileName), @"ThumbnailPhotoFileName", DbType.String, isRequired : false), new ColumnMetadata(nameof(LargePhoto), @"LargePhoto", DbType.Binary, isRequired : false), new ColumnMetadata(nameof(LargePhotoFileName), @"LargePhotoFileName", DbType.String, isRequired : false), new ColumnMetadata(nameof(ModifiedDate), @"ModifiedDate", DbType.DateTime, isRequired : true) ); // Cache entites metadata. EntityMeta <ProductPhoto> .LoadRelationMetadata ( null ); // Cache table metadata. EntityMeta <ProductPhoto> .HasLanguageTable = false; EntityMeta <ProductPhoto> .TableSchema = @"Production"; EntityMeta <ProductPhoto> .TableName = @"ProductPhoto"; EntityMeta <ProductPhoto> .LanguageTableSchema = null; EntityMeta <ProductPhoto> .LanguageTableName = null; }
public string BuildInsertSql(EntityMeta meta, List<string> list) { if (list == null || list.Count == 0) return string.Empty; StringBuilder sqlbuilder = new StringBuilder(); sqlbuilder.AppendFormat("INSERT INTO [{0}] (", meta.TableName); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (meta.Columns[i].Identity || !list.Contains(meta.Columns[i].ColumnName)) { continue; } if (j > 0) { sqlbuilder.Append(","); } sqlbuilder.Append("[" + meta.Columns[i].ColumnName + "]"); j++; } sqlbuilder.Append(") VALUES ("); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (meta.Columns[i].Identity || !list.Contains(meta.Columns[i].ColumnName)) { continue; } if (j > 0) { sqlbuilder.Append(","); } sqlbuilder.Append("@" + meta.Columns[i].ColumnName + ""); j++; } sqlbuilder.Append(");"); if (meta.Columns.Exists(x => x.Identity)) { sqlbuilder.Append(" SELECT CAST(SCOPE_IDENTITY() as bigint) as Id;"); //sqlbuilder.Append("SELECT SCOPE_IDENTITY();"); } else { sqlbuilder.Append(" SELECT CAST(0 as bigint) as Id;"); } return sqlbuilder.ToString(); }
public string BuildInsertSql(EntityMeta meta) { if (meta.Columns == null || meta.Columns.Count == 0) return string.Empty; StringBuilder sqlbuilder = new StringBuilder(); sqlbuilder.AppendFormat("INSERT INTO `{0}` (", meta.TableName); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (meta.Columns[i].Identity) { continue; } if (j > 0) { sqlbuilder.Append(","); } sqlbuilder.Append("`" + meta.Columns[i].ColumnName + "`"); j++; } sqlbuilder.Append(") VALUES ("); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (meta.Columns[i].Identity) { continue; } if (j > 0) { sqlbuilder.Append(","); } sqlbuilder.Append("@" + meta.Columns[i].ColumnName + ""); j++; } sqlbuilder.Append(");"); if (meta.Columns.Exists(x => x.Identity)) { sqlbuilder.Append("SELECT CAST(LAST_INSERT_ID() AS SIGNED);"); } else { sqlbuilder.Append(" SELECT CAST(0 AS SIGNED);"); } return sqlbuilder.ToString(); }
private void AddEntity(EntityMeta meta) { var allProperties = meta.ManagedProperties.GetCompiledProperties(); //添加这个类型以及它的基类型 var types = TypeHelper.GetHierarchy(meta.EntityType, typeof(Entity)); foreach (var type in types) { //如果还没有添加了这个类型,则尝试添加到结果中。 var exist = _result.EntityTypes.Find(type.FullName); if (exist == null) { var properties = allProperties.Where(mp => mp.DeclareType == type).ToArray(); AddEntity(type, properties); } } }
private static void ProcessManagedProperties(Type type, PersistanceTableInfo table, EntityMeta em) { foreach (var property in em.EntityProperties) { var meta = property.ColumnMeta; if (meta == null) continue; var propertyName = property.Name; //生成 ManagedPropertyBridge var epm = em.Property(propertyName); if (epm == null) { throw new ArgumentNullException(string.Format("{0}.{1} 属性需要使用托管属性进行编写。", type.FullName, propertyName)); } var mp = epm.ManagedProperty as IProperty; //列名 var columnName = meta.ColumnName; if (string.IsNullOrWhiteSpace(columnName)) columnName = propertyName; var column = new PersistanceColumnInfo { Table = table, Name = columnName, DataType = epm.PropertyType, IsIdentity = meta.IsIdentity, IsPrimaryKey = meta.IsPrimaryKey, Property = mp }; if (meta.IsPrimaryKey) { table.PKColumn = column; } table.Columns.Add(column); } }
/// <summary> /// 删除不必要的对象,只留下需要保存的“脏”数据 /// </summary> /// <param name="diffEntity">The difference entity.</param> /// <param name="entityInfo">The entity information.</param> protected virtual void ClearDataCore(Entity diffEntity, EntityMeta entityInfo) { foreach (var item in entityInfo.ChildrenProperties) { var mp = item.ManagedProperty; //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值 if (!diffEntity.FieldExists(mp)) continue; var children = diffEntity.GetProperty(mp) as EntityList; if (children == null) continue; for (int i = children.Count - 1; i >= 0; i--) { var child = children[i]; if (!child.IsDirty) { children.Remove(child); children.DeletedList.Remove(child); } else { this.ClearData(child); } } } }
/// <summary> /// 清除子对象集合 /// </summary> /// <param name="oldEntity">The old entity.</param> /// <param name="entityInfo">The entity information.</param> protected virtual void MakeSavedCore(Entity oldEntity, EntityMeta entityInfo) { foreach (var item in entityInfo.ChildrenProperties) { var mp = item.ManagedProperty as IListProperty; //如果是懒加载属性,并且没有加载数据时,不需要遍历此属性值 if (!oldEntity.FieldExists(mp)) continue; var children = oldEntity.GetProperty(mp) as EntityList; if (children == null) continue; //清除已删除数据 children.CastTo<EntityList>().DeletedList.Clear(); //所有子对象,都标记为已保存 for (int i = children.Count - 1; i >= 0; i--) { var child = children[i] as Entity; if (child.IsDirty || child.IsNew) MakeSaved(child); } } oldEntity.MarkSaved(); }
/// <summary> /// 根据实体类型创建表的描述信息,并添加到数据库中 /// </summary> /// <param name="em">The memory.</param> /// <exception cref="System.ArgumentNullException"></exception> /// <exception cref="System.InvalidOperationException">refMeta.ReferenceInfo == null</exception> private void BuildTable(EntityMeta em) { var tableMeta = em.TableMeta; //视图类不需要支持数据库迁移。 if (tableMeta.IsMappingView) { return; } var table = new Table(tableMeta.TableName, this.Database); var metaProperties = em.EntityProperties; //var managedProperties = ManagedPropertyRepository.Instance // .GetTypePropertiesContainer(em.EntityType) // .GetNonReadOnlyCompiledProperties(); foreach (var property in metaProperties) { var columnMeta = property.ColumnMeta; if (columnMeta == null) continue; var mp = property.ManagedProperty; if (mp == null) { throw new ArgumentNullException(string.Format("{0}.{1} 属性需要使用托管属性进行编写。", em.Name, mp.Name)); } //列名 var propertyName = property.Name; var columnName = columnMeta.ColumnName; if (string.IsNullOrWhiteSpace(columnName)) columnName = propertyName; //类型 var propertyType = property.PropertyType; bool isNullableRef = false; #region 引用关系 if (columnMeta.HasFKConstraint) { var refProperty = mp as IRefProperty; if (refProperty != null) { isNullableRef = refProperty.Nullable; var refMeta = em.Property(refProperty.RefEntityProperty); if (refMeta.ReferenceInfo == null) throw new InvalidOperationException("refMeta.ReferenceInfo == null"); //引用实体的类型。 var refTypeMeta = refMeta.ReferenceInfo.RefTypeMeta; if (refTypeMeta != null) { var refTableMeta = refTypeMeta.TableMeta; if (refTableMeta != null) { //如果主键表已经被忽略,那么到这个表上的外键也不能建立了。 //这是因为被忽略的表的结构是未知的,不一定是以这个字段为主键。 if (!this.Database.IsIgnored(refTableMeta.TableName)) { var id = refTypeMeta.Property(Entity.IdProperty); //有时一些表的 Id 只是自增长,但并不是主键,不能创建外键。 if (id.ColumnMeta.IsPrimaryKey) { this._foreigns.Add(new ForeignConstraintInfo() { FkTableName = tableMeta.TableName, PkTableName = refTableMeta.TableName, FkColumn = columnName, PkColumn = id.ColumnMeta.ColumnName, NeedDeleteCascade = refProperty.ReferenceType == ReferenceType.Parent }); } } } } } else if (mp == Entity.TreePIdProperty) { var id = em.Property(Entity.IdProperty); //有时一些表的 Id 只是自增长,但并不是主键,不能创建外键。 if (id.ColumnMeta.IsPrimaryKey) { this._foreigns.Add(new ForeignConstraintInfo() { FkTableName = tableMeta.TableName, PkTableName = tableMeta.TableName, FkColumn = columnName, PkColumn = id.ColumnMeta.ColumnName, NeedDeleteCascade = false }); } } } #endregion var dataType = TypeHelper.IgnoreNullable(propertyType); //对于支持多数据类型的 Id、TreePId 属性进行特殊处理。 if (mp == Entity.IdProperty || mp == Entity.TreePIdProperty) { dataType = em.IdType; } var dbType = columnMeta.DataType.GetValueOrDefault(DbTypeHelper.ConvertFromCLRType(dataType)); var column = new Column(dbType, columnMeta.DataTypeLength, columnName, table); if (columnMeta.IsRequired.HasValue) { column.IsRequired = columnMeta.IsRequired.Value; } else { column.IsRequired = !isNullableRef && !propertyType.IsClass && !TypeHelper.IsNullable(propertyType); } //IsPrimaryKey 的设置放在 IsRequired 之后,可以防止在设置可空的同时把列调整为非主键。 column.IsPrimaryKey = columnMeta.IsPrimaryKey; column.IsIdentity = columnMeta.IsIdentity; table.Columns.Add(column); } table.SortColumns(); this.AddTable(table); }
public string BuildUpdateSql(EntityMeta meta, List<string> list) { if (list == null || list.Count == 0) return string.Empty; var keys = meta.Columns.FindAll(_ => _.PrimaryKey); StringBuilder sqlbuilder = new StringBuilder(); sqlbuilder.AppendFormat("UPDATE [{0}] SET ", meta.TableName); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (!meta.Columns[i].PrimaryKey && list.Contains(meta.Columns[i].ColumnName)) { if (j > 0) { sqlbuilder.Append(","); } j++; sqlbuilder.Append("[" + meta.Columns[i].ColumnName + "]=@" + meta.Columns[i].ColumnName + ""); } } sqlbuilder.Append(" WHERE "); for (int i = 0; i < keys.Count; i++) { if (i > 0) { sqlbuilder.Append(" AND "); } sqlbuilder.Append("[" + keys[i].ColumnName + "]=@" + keys[i].ColumnName); } return sqlbuilder.ToString(); }
private static void MapDefaultColumns(EntityMeta meta) { if (meta.TableMeta != null) { foreach (var ep in meta.EntityProperties) { var mp = ep.ManagedProperty; //Id 属性,默认的元数据中,即是主键,也是自增长列。应用层可以考虑再做配置。 if (mp == EntityConvention.Property_Id) { ep.ColumnMeta = new ColumnMeta { ColumnName = mp.Name, IsPrimaryKey = true, }; if (meta.IdType == typeof(int) || meta.IdType == typeof(long)) { ep.ColumnMeta.IsIdentity = true; } } //其它属性 else if (mp != EntityConvention.Property_TreeIndex && mp != EntityConvention.Property_TreePId) { var clrProperty = ep.CLRProperty; if (clrProperty != null) { var columnAttri = clrProperty.GetSingleAttribute<ColumnAttribute>(); if (columnAttri != null) { var name = columnAttri.ColumnName; if (string.IsNullOrWhiteSpace(name)) name = clrProperty.Name; ep.ColumnMeta = new ColumnMeta { ColumnName = name }; } } } } //树的两个属性 if (meta.IsTreeEntity) { var p = meta.Property(EntityConvention.Property_TreeIndex); if (p != null) { p.MapColumn(); } p = meta.Property(EntityConvention.Property_TreePId); if (p != null) { p.MapColumn(); } } } }
/// <summary> /// 创建某个实体类的视图元数据 /// </summary> /// <param name="entityMeta"></param> /// <returns></returns> private EntityViewMeta CreateEntityViewMeta(EntityMeta entityMeta) { var entityType = entityMeta.EntityType; var vm = RafyEnvironment.Location.IsWebUI ? new WebEntityViewMeta() : new WPFEntityViewMeta() as EntityViewMeta; vm.EntityMeta = entityMeta; this.CreatePropertiesViewMeta(vm); return vm; }
internal void UseDefaultMeta() { this._meta = null; }
/// <summary> /// 懒加载 RefTypeMeta 属性。 /// </summary> private void InitLazyRefTypeMeta() { if (this._RefTypeMeta == null) { this._RefTypeMeta = CommonModel.Entities.Find(this.RefType); } }
/// <summary> /// 从一个实体类型读取它所对应的视图模型。 /// </summary> /// <param name="meta"></param> /// <returns></returns> internal EntityViewMeta Read(EntityMeta meta) { var evm = this.CreateEntityViewMeta(meta); return evm; }
public string BuildReplaceInsertSQL(EntityMeta meta) { if (meta.Columns == null || meta.Columns.Count == 0) return string.Empty; StringBuilder sqlbuilder = new StringBuilder(); sqlbuilder.AppendFormat("REPLACE INTO `{0}` (", meta.TableName); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (meta.Columns[i].Identity) { continue; } if (j > 0) { sqlbuilder.Append(","); } sqlbuilder.Append("`" + meta.Columns[i].ColumnName + "`"); j++; } sqlbuilder.Append(") VALUES ("); for (int i = 0, j = 0; i < meta.Columns.Count; i++) { if (meta.Columns[i].Identity) { continue; } if (j > 0) { sqlbuilder.Append(","); } sqlbuilder.Append("@" + meta.Columns[i].ColumnName + ""); j++; } sqlbuilder.Append(");"); return sqlbuilder.ToString(); }
public EntityPropertySetter(EntityMeta entityMeta) { _entityMeta = entityMeta; _refIdProperties = _entityMeta.ManagedProperties .GetAvailableProperties().OfType<IRefIdProperty>().ToArray(); }