Skip to content

Firebird 数据库级联保存报 NullReferenceException 异常 #2023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ecocom opened this issue May 6, 2025 · 6 comments
Open

Firebird 数据库级联保存报 NullReferenceException 异常 #2023

ecocom opened this issue May 6, 2025 · 6 comments

Comments

@ecocom
Copy link

ecocom commented May 6, 2025

问题描述及重现代码:

Firebird 数据库利用导航属性进行级联保存时报 System.NullReferenceException:

var repo = fsql.GetRepository<TestCategory>();
repo.DbContextOptions.EnableCascadeSave = true;
repo.Insert(list); // System.NullReferenceException

在执行导出导入时发现的问题(数据从某库导出并导入到目标库),目标库是 pgsql/sqlserver/mysql/oracle/dm 等等时均正常,只有 Firebird 导入失败,搞好长时间才发现实体里的日期类型属性加上 [Column(ServerTime = DateTimeKind.Local)] 后插入就报异常,完整重现见下。

using FreeSql.DataAnnotations;
using System.ComponentModel.DataAnnotations;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            IFreeSql fsql = new FreeSql.FreeSqlBuilder()
                .UseConnectionString(FreeSql.DataType.Firebird, "database=localhost/3050:EMPLOYEE.FDB;user=SYSDBA;password=123456")
                .UseAutoSyncStructure(true)
                .UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
                .UseMonitorCommand(cmd => Console.WriteLine($"-- sql@{DateTime.Now:HH:mm:ss.fff} --\n{cmd.CommandText}\n---------\n"))
                .Build();

            var list = new List<TestCategory>
            {
                new TestCategory
                {
                    Name = "c1",
                    Articles = new List<TestArticle>
                    {
                        new TestArticle
                        {
                            Title = "t1a",
                        },
                        new TestArticle
                        {
                            Title = "t1b",
                        }
                    }
                },
                new TestCategory
                {
                    Name = "c2",
                    Articles = new List<TestArticle>
                    {
                        new TestArticle
                        {
                            Title = "t2a",
                        },
                        new TestArticle
                        {
                            Title = "t2b",
                        }
                    }
                }
            };

            var repo = fsql.GetRepository<TestCategory>();
            repo.DbContextOptions.EnableCascadeSave = true;
            repo.Insert(list); // 这里报 System.NullReferenceException

            Console.ReadLine();
        }
    }

    [Table(Name = "test_category")]
    public class TestCategory
    {
        [Column(IsPrimary = true, IsIdentity = true)]
        public long Id { get; set; }

        [Required]
        public string Name { get; set; }

        [Navigate(nameof(TestArticle.CategoryId))]
        public List<TestArticle> Articles { get; set; } = new List<TestArticle>();
    }

    [Table(Name = "test_article")]
    public class TestArticle
    {
        [Column(IsPrimary = true, IsIdentity = true)]
        public long Id { get; set; }

        [Required]
        public string Title { get; set; }

        public long CategoryId { get; set; }

        [Navigate(nameof(CategoryId))]
        public TestCategory Category { get; set; }

        [Column(ServerTime = DateTimeKind.Local)] // 移除此特性就可以成功插入!
        public DateTime CreatedTime { get; set; }
    }
}

堆栈:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=FreeSql
  StackTrace:
   在 FreeSql.Extensions.EntityUtil.EntityUtilExtensions.MapEntityValue(IFreeSql orm, Type entityType, Object entityFrom, Object entityTo)
   在 FreeSql.DbSet`1.AddPriv(TEntity data, Boolean isCheck)
   在 FreeSql.DbSet`1.AddRange(IEnumerable`1 data)
   在 FreeSql.DbSet`1.<>c__DisplayClass81_0.<AddOrUpdateNavigate>b__0(PropertyInfo prop)
   在 FreeSql.DbSet`1.AddOrUpdateNavigate(TEntity item, Boolean isAdd, String propertyName)
   在 FreeSql.DbSet`1.AddPriv(TEntity data, Boolean isCheck)
   在 FreeSql.DbSet`1.AddRange(IEnumerable`1 data)
   在 FreeSql.BaseRepository`1.Insert(IEnumerable`1 entitys)
   在 ConsoleApp3.Program.Main(String[] args) 在 E:\QuickAdmin\source\FreeSql\issues\ConsoleApp3\Program.cs 中: 第 53 行

数据库版本

Firebird 5.0

安装的Nuget包

FreeSql.Provider.Firebird/FreeSql.DbContext 3.5.108
更新至 3.5.205 问题依旧

.net framework/. net core? 及具体版本

.net8.0

@ecocom
Copy link
Author

ecocom commented May 6, 2025

看了看 MapEntityValue 源码,找不出所以然,应该是 Lambda 内有了 NullReferenceException,不知道怎会与 [Column(ServerTime = DateTimeKind.Local)] 有关系

@2881099
Copy link
Collaborator

2881099 commented May 6, 2025

Firebird Inserted 实现有 bug,与级联冲突,待会发新版本

@2881099
Copy link
Collaborator

2881099 commented May 6, 2025

FreeSql.Provider.Firebird v3.5.206-preview20250506

过几分钟后更新,可解决

@2881099
Copy link
Collaborator

2881099 commented May 6, 2025

错误以前发生过 #362,检查过了,其他数据库代码没问题,唯有 firebird 漏掉了没改。

@ecocom
Copy link
Author

ecocom commented May 6, 2025

好的,辛苦了

@ecocom
Copy link
Author

ecocom commented May 6, 2025

更新了,问题解决

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants