linq to sql 动态表名,修改表映射,EF是否支持

微软想用EF代替linq to sql,但是目前很多现实问题无法解决

首先是in,原来linq to sql可以用contain,但EF没有

EF也不支持标准sql了

第二是 动态表名

原来linq to sql可以重写MappingSource,再把MappingSource实例传给datacontext,就可以动态修改类与表的对应关系

代码如下

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();
        }
    }
}

现在在找EF修改表映射的方法


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值