当前位置: 首页 > news >正文

网站开发科普书海口仿站定制模板建站

网站开发科普书,海口仿站定制模板建站,云虚拟主机可以做多少个网站,临沂城市建设网站文章以efcore 2.0.0-preview2.测试验证通过。其他版本不保证使用#xff0c;但是思路不会差太远。源代码,报道越短#xff0c;事情越严重#xff01;文章越短#xff0c;内容越精悍#xff01; 目标#xff1a;1.实现entity的自动发现和mapper设置.2.默认字符串长度…文章以efcore 2.0.0-preview2.测试验证通过。其他版本不保证使用但是思路不会差太远。源代码,报道越短事情越严重文章越短内容越精悍 目标1.实现entity的自动发现和mapper设置.2.默认字符串长度而不是nvarchar(max).3.decimal设置精度 实现目标1继承RelationalModelCustomizer重写Customize方法。 当然我们也可以重写dbcontext的OnModelCreating方法but我们怎么能这么low呢。必须要用点高级玩意是吧当然这也是更底层的扩展方式。项目里面有多个dbcontext的话在这里集中扩展管理比较方便。在然后这个RelationalModelCustomizer继承自ModelCustomizer。在联想到efcore未来的版本会支持redisnosql什么的。到时候估计还回有一个osqlModelCustomizer之类的吧期待中...... 实现目标2、3继承自CoreConventionSetBuilder类重写CreateConventionSet方法。 重写CreateConventionSet方法能拿到关键的ConventionSet对象。这个对象囊括了诸多的约定配置等等。比如maxlengthattribute属性标记stringlength属性标记timestamp属性标记表id的自动发现规则等等等。。。那我们增加2个小小的约定字符串默认长度StringDefaultLengthConvention和decimal精度设置attribute(DecimalPrecisionAttributeConvention)及fluntapi方式. 文章的最后附efcore 所有的可替换扩展service。 //servie,DI注入替换.services.AddSingletonIModelCustomizer, MyRelationalModelCustomizer(); services.AddSingletonICoreConventionSetBuilder, MyCoreConventionSetBuilder();//实现entity的自动发现和mapper设置public class MyRelationalModelCustomizer : RelationalModelCustomizer{    public MyRelationalModelCustomizer(ModelCustomizerDependencies dependencies)        : base(dependencies){}     public override void Customize(ModelBuilder modelBuilder, DbContext dbContext)    {           base.Customize(modelBuilder, dbContext);             var sp (IInfrastructureIServiceProvider)dbContext;               var dbOptions sp.Instance.GetServicesDbContextOptions();        foreach (var item in dbOptions){                      if (item.ContextType dbContext.GetType())ConfigureDbContextEntityService.Configure(modelBuilder, item, dbContext);}} }public class MyCoreConventionSetBuilder : CoreConventionSetBuilder{    public MyCoreConventionSetBuilder(CoreConventionSetBuilderDependencies dependencies) : base(dependencies){}       public override ConventionSet CreateConventionSet()    {            var conventionSet base.CreateConventionSet();               //默认字符串长度而不是nvarchar(max).//为什么要insert(0,obj),则是因为这个默认规则要最优先处理如果后续有规则的话就直接覆盖了。//propertyBuilder.HasMaxLength(32, ConfigurationSource.Convention);//理论上我指定了规则的级别为.Convention应该和顺序就没有关系了。but还没有完成测试所以我也不知道conventionSet.PropertyAddedConventions.Insert(0, new StringDefaultLengthConvention());        //decimal设置精度conventionSet.PropertyAddedConventions.Add(new DecimalPrecisionAttributeConvention());                  return conventionSet;} } 下面是StringDefaultLengthConvention和DecimalPrecisionAttributeConvention的实现代码 //字符串默认长度public class StringDefaultLengthConvention : IPropertyAddedConvention {         public InternalPropertyBuilder Apply(InternalPropertyBuilder propertyBuilder)    {        if (propertyBuilder.Metadata.ClrType typeof(string))propertyBuilder.HasMaxLength(32, ConfigurationSource.Convention);          return propertyBuilder;} }//attribute方式设置decimal精度public class DecimalPrecisionAttributeConvention : PropertyAttributeConventionDecimalPrecisionAttribute {    public override InternalPropertyBuilder Apply(InternalPropertyBuilder propertyBuilder, DecimalPrecisionAttribute attribute, MemberInfo clrMember)    {         if (propertyBuilder.Metadata.ClrType typeof(decimal))propertyBuilder.HasPrecision(attribute.Precision, attribute.Scale);          return propertyBuilder;}    /// summary/// decimal类型设置精度/// /summary/// param namepropertyBuilder/param/// param nameprecision精度/param/// param namescale小数位数/parampublic static PropertyBuilderTProperty HasPrecisionTProperty(this PropertyBuilderTProperty propertyBuilder, int precision 18, int scale 4) {    //fluntapi方式设置精度  ((IInfrastructureInternalPropertyBuilder)propertyBuilder).Instance.HasPrecision(precision, scale);        return propertyBuilder; }      public static InternalPropertyBuilder HasPrecision(this InternalPropertyBuilder propertyBuilder, int precision, int scale){propertyBuilder.Relational(ConfigurationSource.Explicit).HasColumnType($decimal({precision},{scale}));          return propertyBuilder; } 以上就是实现的代码就这么几行。嗯还是挺简单的. -------------- 以下则是efcore的源代码。展示了如此之多的可扩展。随着DI的运用和微软的开放嗯。用烂啦用炸拉(^_^) //各种规则和约定public virtual ConventionSet AddConventions(ConventionSet conventionSet){ValueGeneratorConvention valueGeneratorConvention new RelationalValueGeneratorConvention();ReplaceConvention(conventionSet.BaseEntityTypeChangedConventions, valueGeneratorConvention);ReplaceConvention(conventionSet.PrimaryKeyChangedConventions, valueGeneratorConvention);ReplaceConvention(conventionSet.ForeignKeyAddedConventions, valueGeneratorConvention);ReplaceConvention(conventionSet.ForeignKeyRemovedConventions, valueGeneratorConvention);    var relationalColumnAttributeConvention new RelationalColumnAttributeConvention();conventionSet.PropertyAddedConventions.Add(relationalColumnAttributeConvention);     var sharedTableConvention new SharedTableConvention();conventionSet.EntityTypeAddedConventions.Add(new RelationalTableAttributeConvention());conventionSet.EntityTypeAddedConventions.Add(sharedTableConvention);conventionSet.BaseEntityTypeChangedConventions.Add(new DiscriminatorConvention());conventionSet.BaseEntityTypeChangedConventions.Add(          new TableNameFromDbSetConvention(Dependencies.Context?.Context, Dependencies.SetFinder));conventionSet.EntityTypeAnnotationChangedConventions.Add(sharedTableConvention);conventionSet.PropertyFieldChangedConventions.Add(relationalColumnAttributeConvention);conventionSet.PropertyAnnotationChangedConventions.Add((RelationalValueGeneratorConvention)valueGeneratorConvention);conventionSet.ForeignKeyUniquenessChangedConventions.Add(sharedTableConvention);conventionSet.ForeignKeyOwnershipChangedConventions.Add(sharedTableConvention);conventionSet.ModelBuiltConventions.Add(new RelationalTypeMappingConvention(Dependencies.TypeMapper));conventionSet.ModelBuiltConventions.Add(sharedTableConvention);conventionSet.ModelAnnotationChangedConventions.Add(new RelationalDbFunctionConvention());         return conventionSet; } //还是各种规则和约定public virtual ConventionSet CreateConventionSet(){    var conventionSet new ConventionSet();      var propertyDiscoveryConvention new PropertyDiscoveryConvention(Dependencies.TypeMapper);        var keyDiscoveryConvention new KeyDiscoveryConvention();         var inversePropertyAttributeConvention new InversePropertyAttributeConvention(Dependencies.TypeMapper);          var relationshipDiscoveryConvention new RelationshipDiscoveryConvention(Dependencies.TypeMapper);conventionSet.EntityTypeAddedConventions.Add(new NotMappedEntityTypeAttributeConvention());conventionSet.EntityTypeAddedConventions.Add(new NotMappedMemberAttributeConvention());conventionSet.EntityTypeAddedConventions.Add(new BaseTypeDiscoveryConvention());conventionSet.EntityTypeAddedConventions.Add(propertyDiscoveryConvention);conventionSet.EntityTypeAddedConventions.Add(keyDiscoveryConvention);conventionSet.EntityTypeAddedConventions.Add(inversePropertyAttributeConvention);conventionSet.EntityTypeAddedConventions.Add(relationshipDiscoveryConvention);conventionSet.EntityTypeAddedConventions.Add(new DerivedTypeDiscoveryConvention());conventionSet.EntityTypeIgnoredConventions.Add(inversePropertyAttributeConvention);             var foreignKeyIndexConvention new ForeignKeyIndexConvention();    var valueGeneratorConvention new ValueGeneratorConvention();conventionSet.BaseEntityTypeChangedConventions.Add(propertyDiscoveryConvention);conventionSet.BaseEntityTypeChangedConventions.Add(keyDiscoveryConvention);conventionSet.BaseEntityTypeChangedConventions.Add(inversePropertyAttributeConvention);conventionSet.BaseEntityTypeChangedConventions.Add(relationshipDiscoveryConvention);conventionSet.BaseEntityTypeChangedConventions.Add(foreignKeyIndexConvention);conventionSet.BaseEntityTypeChangedConventions.Add(valueGeneratorConvention );    // An ambiguity might have been resolvedconventionSet.EntityTypeMemberIgnoredConventions.Add(inversePropertyAttributeConvention);conventionSet.EntityTypeMemberIgnoredConventions.Add(relationshipDiscoveryConvention);              var keyAttributeConvention new KeyAttributeConvention();               var foreignKeyPropertyDiscoveryConvention new ForeignKeyPropertyDiscoveryConvention();                 var backingFieldConvention new BackingFieldConvention();    var concurrencyCheckAttributeConvention new ConcurrencyCheckAttributeConvention();                   var databaseGeneratedAttributeConvention new DatabaseGeneratedAttributeConvention();                   var requiredPropertyAttributeConvention new RequiredPropertyAttributeConvention();                   var maxLengthAttributeConvention new MaxLengthAttributeConvention();                     var stringLengthAttributeConvention new StringLengthAttributeConvention();                    var timestampAttributeConvention new TimestampAttributeConvention();conventionSet.PropertyAddedConventions.Add(backingFieldConvention);conventionSet.PropertyAddedConventions.Add(concurrencyCheckAttributeConvention);conventionSet.PropertyAddedConventions.Add(databaseGeneratedAttributeConvention);conventionSet.PropertyAddedConventions.Add(requiredPropertyAttributeConvention);conventionSet.PropertyAddedConventions.Add(maxLengthAttributeConvention);conventionSet.PropertyAddedConventions.Add(stringLengthAttributeConvention);conventionSet.PropertyAddedConventions.Add(timestampAttributeConvention);conventionSet.PropertyAddedConventions.Add(keyDiscoveryConvention);conventionSet.PropertyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.PropertyAddedConventions.Add(keyAttributeConvention);conventionSet.PrimaryKeyChangedConventions.Add(valueGeneratorConvention);conventionSet.KeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.KeyAddedConventions.Add(foreignKeyIndexConvention);conventionSet.KeyRemovedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.KeyRemovedConventions.Add(foreignKeyIndexConvention);conventionSet.KeyRemovedConventions.Add(keyDiscoveryConvention);                      var cascadeDeleteConvention new CascadeDeleteConvention();conventionSet.ForeignKeyAddedConventions.Add(new ForeignKeyAttributeConvention(Dependencies.TypeMapper));conventionSet.ForeignKeyAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.ForeignKeyAddedConventions.Add(keyDiscoveryConvention);conventionSet.ForeignKeyAddedConventions.Add(valueGeneratorConvention );conventionSet.ForeignKeyAddedConventions.Add(cascadeDeleteConvention);conventionSet.ForeignKeyAddedConventions.Add(foreignKeyIndexConvention);conventionSet.ForeignKeyRemovedConventions.Add(keyDiscoveryConvention);conventionSet.ForeignKeyRemovedConventions.Add(valueGeneratorConvention );conventionSet.ForeignKeyRemovedConventions.Add(foreignKeyIndexConvention);conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.ForeignKeyUniquenessChangedConventions.Add(foreignKeyIndexConvention);conventionSet.ForeignKeyOwnershipChangedConventions.Add(new NavigationEagerLoadingConvention());conventionSet.ModelBuiltConventions.Add(new ModelCleanupConvention());conventionSet.ModelBuiltConventions.Add(keyAttributeConvention);conventionSet.ModelBuiltConventions.Add(new IgnoredMembersValidationConvention());conventionSet.ModelBuiltConventions.Add(new PropertyMappingValidationConvention(Dependencies.TypeMapper));conventionSet.ModelBuiltConventions.Add(new RelationshipValidationConvention());conventionSet.ModelBuiltConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.NavigationAddedConventions.Add(backingFieldConvention);conventionSet.NavigationAddedConventions.Add(new RequiredNavigationAttributeConvention());conventionSet.NavigationAddedConventions.Add(inversePropertyAttributeConvention);conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.NavigationAddedConventions.Add(relationshipDiscoveryConvention);conventionSet.NavigationRemovedConventions.Add(relationshipDiscoveryConvention);conventionSet.IndexAddedConventions.Add(foreignKeyIndexConvention);conventionSet.IndexRemovedConventions.Add(foreignKeyIndexConvention);conventionSet.IndexUniquenessChangedConventions.Add(foreignKeyIndexConvention);conventionSet.PropertyNullabilityChangedConventions.Add(cascadeDeleteConvention);conventionSet.PrincipalEndChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.PropertyFieldChangedConventions.Add(keyDiscoveryConvention);conventionSet.PropertyFieldChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);conventionSet.PropertyFieldChangedConventions.Add(keyAttributeConvention);conventionSet.PropertyFieldChangedConventions.Add(concurrencyCheckAttributeConvention);conventionSet.PropertyFieldChangedConventions.Add(databaseGeneratedAttributeConvention);conventionSet.PropertyFieldChangedConventions.Add(requiredPropertyAttributeConvention);conventionSet.PropertyFieldChangedConventions.Add(maxLengthAttributeConvention);conventionSet.PropertyFieldChangedConventions.Add(stringLengthAttributeConvention);conventionSet.PropertyFieldChangedConventions.Add(timestampAttributeConvention);    return conventionSet; } 我就是所有的可替换service啦。不要眼花有点多找着合适的用起来 serviceCollection.AsQueryable().Where(p p.ServiceType.ToString().StartsWith(Microsoft.EntityFrameworkCore)).Each(sd {Console.WriteLine(${sd.Lifetime.ToString().PadRight(15, )}{sd.ServiceType.FullName}); }); 原文地址http://www.cnblogs.com/calvinK/p/7234872.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注
http://wiki.neutronadmin.com/news/393491/

相关文章:

  • 本地网站搭建流程网站域名费怎么查询
  • 如何推广网站架构少儿编程加盟品牌有哪些
  • 用pyton可以做网站吗禹州市门户网站建设
  • 天津网站建设技术托管推广网络公司
  • 网站 乱码最新购物网站建设框架
  • 铜梁城乡建设网站单页网站内链接
  • 广州知名网站建设哪家好软件商店电脑版下载
  • 宝山网站建设 网站外包wordpress更换域名首页无法访问
  • wordpress的aware主题广州 关于进一步优化
  • ins做甜品网站wordpress 招聘模块
  • 郑州营销型网站建设价格福州市住房和城乡建设部网站
  • 响应式网站pad尺寸wordpress二级菜单排列
  • 仪征市城乡建设局网站四川seo技术培训
  • 郑州网站建设公司qq做海报一般都去什么网站看
  • 江苏省建设厅网站资质升级网站流量如何盈利
  • 来宾住房和城乡建设局网站青海格尔木建设局网站
  • 在vs上用c 做登录网站wordpress个人资料
  • 公众号网站怎么做百度一下京东
  • 营商环境建设监督局网站企业网站托管一个月多少钱
  • 关于做网站的了解点企业做网站的
  • 网站做百度竞价引流费用多少钱企业运营模拟实践报告
  • 个性化网站建设多少钱运营推广渠道有哪些
  • 山东省建设工程执业资格中心网站网站建设后运维合同
  • html网页代码编辑器优化公司排行榜
  • 网站关键词优化多少钱wordpress博客下载插件
  • 广州比较好的网站建设哪家好重庆企业网站开发服务器
  • 产品宣传网站模板查询网站服务器地址
  • 网页制作与设计元素是什么汕头市做网站优化
  • 济南网站建设小程序平面广告设计论文
  • 网站曝光率文创产品创意设计