【问题标题】:When I am querying data from database it doesn't passes to the base class in C# and SQLite当我从数据库中查询数据时,它不会传递给 C# 和 SQLite 中的基类
【发布时间】:2022-01-15 19:10:39
【问题描述】:

我有一个类Customer,它继承自类Person,但是当我从数据库中查询数据时,它不会传递给基类Person。数据只是传递给Customer 类。

CustomerInfo 数据库表数据有列:

Id - FirstName - LastName - NickName - Address - RegistrationDate

我使用 Dapper 连接到我的 SQLite 数据库。

为什么会这样,我想把数据传给构造函数但是不知道怎么做。

public class PersonModel
{
    int Id;
    string FirstName;
    string LastName;

    public PersonModel() { }

    public PersonModel(string firstName, string lastName, int id = 0)
    {
        Id = id;
        FirstName = firstName;
        LastName = lastName;
    }

    public string GetFullName()
    {
        return $"{FirstName} {LastName}";
    }
}

public class CustomerModel : PersonModel
{
    string NickName;
    string Address;
    string RegistrationDate;

    public CustomerModel() { }

    public CustomerModel(string firstName, string lastName,
        string address, string registrationDate = "",
        string nickName = "", int id = 0) : base(firstName, lastName, id)
    {
        NickName = nickName;
        Address = address;
        RegistrationDate = registrationDate;
    }

    public string FullInfo
    {
        get
        {
            return $"{GetFullName()} {RegistrationDate}";
        }
    }
}

public class CustomerDataAccess
{
    public static List<CustomerModel> LoadCustomers()
    {
        using (IDbConnection cnn = new SQLiteConnection(LoadConnectionStrings()))
        {
            IEnumerable<CustomerModel> output = cnn.Query<CustomerModel>("SELECT * FROM CustomerInfo", new DynamicParameters());
            
            return output.ToList();
        }
    }

    private static string LoadConnectionStrings(string id = "Default")
    {
        return ConfigurationManager.ConnectionStrings[id].ConnectionString;
    }
}

【问题讨论】:

    标签: c# sqlite inheritance dapper


    【解决方案1】:

    其中一个问题是您有一个默认构造函数和一个参数化构造函数。 Dapper 使用默认构造函数,这就是为什么 PersonModel 中的字段为空/默认的原因。您可以将客户模型的访问修饰符更改为私有 - private CustomerModel() { } - 它应该选择参数化构造函数。

    但是,当您使用参数化构造函数时,您需要将查询返回的列的顺序与要使用的构造函数中的参数顺序匹配。使用select * from...时,无法保证返回列的顺序。

    因此,您应该将 SQL 查询更新为:

    SELECT FirstName, LastName, Address, RegistrationDate, Nickname, Id FROM CustomerInfo
    

    但是,我确实认为您会从使用公共属性/字段中受益,因为它会在映射数据时为您提供更多选项/控制。

    【讨论】:

    • 我已经尝试过您的解决方案,但问题仍然存在。
    • 您是否尝试过删除两个默认构造函数? @KyrillosGaber
    • @KyrillosGaber 你遇到了什么错误?我在尝试您的解决方案时遇到了错误,很想知道您是否也遇到同样的错误
    • 当我删除构造函数 public CustomerModel() { } 时,我得到了这个错误构造函数或一个匹配的签名(System.String FirstName、System.String LastName、System.String Address、System.String RegestrationDate、System.String NickName , System.Int64 Id) 是 StoreManagementLibrary.CustomerModel 物化所必需的
    • 你没有这样的构造函数吗:public CustomerModel(string firstName, string lastName, string address, string registrationDate = "", string nickName = "", int id = 0)?保留参数化构造函数,但删除无参数构造函数。 SQL 返回的字段顺序应与构造函数@KyrillosGaber 中的参数顺序一致
    【解决方案2】:

    您的查询仅返回 customerinfo 表列,如果您想从两个表中返回信息,它应该与 person 表连接。

    恕我直言,我不知道使用构造函数而不是 getter 设置器会赢得什么。试试这个

    public class PersonModel
    {
            public int Id {get; set;}
            public  string FirstName {get; set;}
            public string LastName {get; set;}
            ....and so on
    }
    

    与其他类相同

    【讨论】:

    • CustomerInfo 数据库表数据为:Id - FirstName - LastName - NickName - Address - RegestrationDate。当我查询数据库时,只有 NickName、Address 和 RegestrationDate 传递给客户类,但 FirstName、LastName 和 Id 没有传递。
    • @KyrillosGaber 正如我所建议的那样,我已经告诉过你,getter 和 setter。忘记构造函数。仅在您不能没有它们的情况下使用它们。
    • 我试图将人员表与客户表分开,但存在同样的问题。
    • 我认为数据自动传递给类时的问题,而不是我想捕获数据并手动传递它。但我不知道怎么做!
    【解决方案3】:

    其中一个问题是 c# 中的 int 数据类型和 sqlite 中的 INTEGER 数据类型不匹配,我通过使用 long 而不是 int 解决了它

    【讨论】:

      猜你喜欢
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2012-07-21
      相关资源
      最近更新 更多