一个实体对应多张数据表,.netcore3.1的linq查出的数据全是null,字符串全是空,guid全是0的问题

项目场景:

刚开始使用的方法是:针对netcore3.1中一个实体对应多张数据表的问题,重写MappingSource中的实体名称和数据表明映射的函数部分
新建类DynamicMappingSource


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 BackEnd_waterinfo.Models
{
    public 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);
        }
    }
}

新建类

        public static DBContext getData(int i)
        {
           var mappingSource = new DynamicMappingSource();

            mappingSource.GetMetaTableName = delegate (Type type)
            {
                return "WA_H_X" + i;
            };
		return new DataContext(Config.sqlPath,mappingSource);
           
            
            
        }

问题描述

例如:通过以上方法发现最后能够实现对于动态数据库表的连接,但是查询到的数据全是null,虽然查询到的数据条数是对的,但是数据内容都不对。


原因分析:

例如:查询数据都是null的原因还是不太清楚,应该是因为在底层映射的时候没有映射好,应该是哪个方法重写给忽略了,不过这个问题算是解决了。


解决方案:

新建类,使用原生sql语句进行数据库的增删改查操作。

 public class HistorySqlConnectTool
    {
        public static List<WA_H_X> getData(int i)
        {
           // var mappingSource = new DynamicMappingSource();

            /*mappingSource.GetMetaTableName = delegate (Type type)
            {
                return "WA_H_X" + i;
            };*/
            var table = "WA_H_X" + i;
            //Console.WriteLine(table); 
            var context = new DataContext(Config.sqlPath);
            try
            {
                var result = context.ExecuteQuery<WA_H_X>(@"select * from " + table).ToList();
                return result;
            }
            catch (Exception ex)
            {
                var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
                logger.Error(ex.ToString());
                return new List<WA_H_X>();
            }
            
            
        }
    }

最后一个实体成功映射到多张数据表中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 3.1是微软开发的一种跨平台的开发框架,可以用于构建各种应用程序,包括Web应用、移动应用、桌面应用等。它具有高性能、可扩展、易于维护等特点,被广泛应用于各个领域。 API(Application Programming Interface)是一种软件架构,用于不同系统之间的互操作和通信。通过使用API,我们可以将不同的应用程序和服务连接起来,实现数据交互和功能共享。 MySQL是一种常用的开源关系型数据库管理系统。它具有高性能、稳定性和安全性,并支持多平台。在.NET Core 3.1中,我们可以通过使用MySQL数据库提供程序来操作MySQL数据库。 在.NET Core 3.1 API中使用MySQL数据库,我们可以按照以下步骤进行: 1. 首先,我们需要安装MySQL数据库,并创建一个数据库,用于存储数据。 2. 接着,我们需要在.NET Core 3.1项目中安装MySQL数据库提供程序。可以使用NuGet包管理器或在项目文件中添加对MySQL提供程序的引用。 3. 在项目中添加对MySQL数据库的连接字符串的配置,包括服务器地址、用户名、密码和数据库名称等。 4. 创建一个数据模型,用于定义数据库中的表和字段。 5. 使用Entity Framework Core来执行数据库操作,包括查询、插入、更新和删除等。 6. 在API的控制器中编写相应的接口,用于处理客户端的请求并与数据库进行交互。 7. 在启动文件中配置Web API的路由和Authentication。 通过以上步骤,我们就可以在.NET Core 3.1的API中使用MySQL数据库。使用MySQL数据库可以提供数据存储和检索的功能,使我们的API可以处理和管理数据。同时,由于.NET Core 3.1的跨平台特性,我们可以在不同的操作系统上部署和运行我们的API,实现应用程序的跨平台和多设备支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值