LinqToSQL下如何实现动态表名的映射
来源:优易学  2011-2-9 12:38:31   【优易学:中国教育考试门户网】   资料下载   IT书店

  publicoverrideIEnumerable<MetaFunction>GetFunctions()
  {
  returnsource.GetFunctions();
  }
  publicoverrideMetaTypeGetMetaType(Typetype)
  {
  returnsource.GetMetaType(type);
  }
  publicoverrideMappingSourceMappingSource
  {
  get{returnsource.MappingSource;}
  }
  publicoverrideTypeContextType
  {
  get{returnsource.ContextType;}
  }
  publicoverridestringDatabaseName
  {
  get{returnsource.DatabaseName;}
  }
  publicoverrideTypeProviderType
  {
  get{returnsource.ProviderType;}
  }
  }
  publicFunc<Type,string>GetMetaTableName;
  protectedoverrideMetaModelCreateModel(TypedataContextType)
  {
  if(dataContextType==null)
  {
  thrownewArgumentNullException("dataContextType");
  }
  returnnewDynamicAttributedMetaModel(this,dataContextType);
  }
  }
  [Table(Name="User")]
  classUser
  {
  [Column]
  publicintID;
  [Column]
  publicstringName;
  }
  classProgram
  {
  staticvoidMain(string[]args)
  {
  varmappingSource=newDynamicMappingSource();
  inti=0;
  mappingSource.GetMetaTableName=delegate(Typetype)
  {
  varatt=type.GetCustomAttributes(typeof(TableAttribute),true).Single()
  asTableAttribute;
  Debug.Assert(att!=null);
  returnatt.Name+i;
  };
  varconstr=@"DataSource=NOTEBOOKSQLEXPRESS;InitialCatalog=DemoDataContext;IntegratedSecurity=True";
  varcontext=newDataContext(constr,mappingSource){Log=Console.Out};
  i=1;
  context.GetTable<User>().Select(o=>o).ToList();
  i=2;
  context.GetTable<User>().Select(o=>o).ToList();
  }
  }
  }

上一页  [1] [2] 

责任编辑:小草

文章搜索:
 相关文章
热点资讯
热门课程培训