Linq To SQL 下实现动态表名的映射
2009-02-07 11:53
435 查看
这段时间代码写累了,写几篇文章舒缓一下,也算是劳逸结合吧。如果觉得我的文章对你有用,请给点掌声,谢谢。
提出问题
前段时间,在博客园里看到有位朋友,问如何实现在动态表名。我们都知道,把一个实体类映谢到表里,应该这样写:
把运行后的结果给大家看一下:
从上图可以看到,表名已经由原来的User分别变为了User1和User2。
提出问题
前段时间,在博客园里看到有位朋友,问如何实现在动态表名。我们都知道,把一个实体类映谢到表里,应该这样写:
using System; using System.Collections.Generic; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Diagnostics; using System.Globalization; using System.Linq; using System.Reflection; using System.Text; using System.Xml.Schema; namespace ALinq.Mapping { class DynamicMappingSource : MappingSource { class DynamicAttributedMetaModel : MetaModel { private MetaModel source; private const string TypeName = "System.Data.Linq.Mapping.AttributedMetaModel"; private DynamicMappingSource mappingSource; internal DynamicAttributedMetaModel(MappingSource mappingSource, Type contextType) { this.mappingSource = (DynamicMappingSource)mappingSource; var bf = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance; var args = new object[] { mappingSource, contextType }; source = typeof(DataContext).Assembly.CreateInstance(TypeName, false, bf, null, args, CultureInfo.CurrentCulture, null) as MetaModel; Debug.Assert(source != null); } public override MetaTable GetTable(Type rowType) { if (mappingSource.GetMetaTableName != null) { var typeName = "System.Data.Linq.Mapping.AttributedMetaTable"; var bf = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance; var attribute = new TableAttribute { Name = mappingSource.GetMetaTableName(rowType) }; var args = new object[] { source, attribute, rowType }; var metaTable = typeof(DataContext).Assembly.CreateInstance(typeName, false, bf, null, args, CultureInfo.CurrentCulture, null) as MetaTable; return metaTable; } return source.GetTable(rowType); } public override MetaFunction GetFunction(MethodInfo method) { return source.GetFunction(method); } public override IEnumerable<MetaTable> GetTables() { return source.GetTables(); } public override IEnumerable<MetaFunction> GetFunctions() { return source.GetFunctions(); } public override MetaType GetMetaType(Type type) { return source.GetMetaType(type); } public override MappingSource MappingSource { get { return source.MappingSource; } } public override Type ContextType { get { return source.ContextType; } } public override string DatabaseName { get { return source.DatabaseName; } } public override Type ProviderType { get { return source.ProviderType; } } } public Func<Type, string> GetMetaTableName; protected override MetaModel CreateModel(Type dataContextType) { if (dataContextType == null) { throw new ArgumentNullException("dataContextType"); } return new DynamicAttributedMetaModel(this, dataContextType); } } [Table(Name = "User")] class User { [Column] public int ID; [Column] public string Name; } class Program { static void Main(string[] args) { var mappingSource = new DynamicMappingSource(); int i = 0; mappingSource.GetMetaTableName = delegate(Type type) { var att = type.GetCustomAttributes(typeof(TableAttribute), true).Single() as TableAttribute; Debug.Assert(att != null); return att.Name + i; }; var constr = @"Data Source=NOTEBOOK\SQLEXPRESS;Initial Catalog=DemoDataContext;Integrated Security=True"; var context = new DataContext(constr, mappingSource) { Log = Console.Out }; i = 1; context.GetTable<User>().Select(o => o).ToList(); i = 2; context.GetTable<User>().Select(o => o).ToList(); } } }
把运行后的结果给大家看一下:
从上图可以看到,表名已经由原来的User分别变为了User1和User2。
相关文章推荐
- LinqToSQL下实现动态表名的映射
- Linq To SQL 下实现动态表名的映射(转载)
- LINQ TO SQL动态修改表名称的实现浅析
- Linq to Sql 动态条件另类实现方法
- Linq to sql 实现多条件的动态查询(方法二)
- Linq to sql 实现多条件的动态查询(方法一)
- linq to sql 动态表名,修改表映射,EF是否支持
- Linq to sql 实现多条件的动态查询(方法一)
- [转]Linq to sql 实现多条件的动态查询(方法二)
- Linq To Sql模式中自动生成T-SQL增删改操作系列~PropertyChanged事件实现在子类中记录属性的变化,在基类中进行统一处理
- linq to sql实现无限递归查询
- Linq to SQL 相对 sql 语句 Not In 的实现。
- LINQ体验(17)——LINQ to SQL语句之动态查询
- Linq To Sql 中使用lambda表达式实现Left Join
- Linq To Sql中实现Left Join与Inner Join使用Linq语法与lambda表达式
- Linq学习笔记一(第一个ORM映射,熟悉一下Linq to sql语法)
- linq to sql 学习(11)linq 构造更完美的动态数据查询(可扩展)
- 学习并使用了两种linq to entity 的实现sql关键字in的查询方法
- linq to sql 实现左(右)连接,那个方法是对的,该怎么处理
- LINQ中实现对象Many-To-Many(多对多)的关系映射