【发布时间】: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