将基本抽象类属性存储在子类数据库表中

本文关键字:子类 数据库 存储 抽象类 属性 | 更新日期: 2023-09-27 18:20:19

我在存储抽象类的属性时遇到了一些问题,构造函数似乎工作得很好。但是,我无法将基本属性存储在子类数据库表中。

public abstract class Vehicle : IComparable< Vehicle >, IComparable {
    public Int16 VehicleID;
    public DateTime ProductionDate;
    public Vehicle(Int16 _ Vehicle ID,DateTime _ProductionDate)
    {
        this.AccidentID = _ AccidentID;
        this.ProductionDate = _ProductionDate;
    }
    int IComparable.CompareTo(object other) {
        return CompareTo((Vehicle)other);
    }
    public int CompareTo(Vehicle other){
        return this.ProductionDate.CompareTo(other.ProductionDate);
    }
    public Vehicle()
    {}
}
public class Car : Vehicle
{
    public Car ()
    {
    }
    public Car (Int16 _VehicleID,DateTime _ProductionDate, Int16 _CarAttribute1, Int16 _CarAttribute2):base(_Vehicle ID,_ProductionDate)
    {
        this.AccidentID = _ AccidentID;
        this.ProductionDate = _ProductionDate;
        this.CarAttribute1 = _CarAttribute1
        this.CarAttribute2 = _CarAttribute2
    }
    [PrimaryKey, AutoIncrement, Column("Attribute1")]
    public Int16 CarAttribute1{ get; set;}
    [Column("Attribute2")]
    public Int16 CarAttribute2{ get; set;}
}

我对C#还很陌生,所以非常感谢您的指导:)我错过了什么?

将基本抽象类属性存储在子类数据库表中

在基类中,应该使用属性而不是字段,因此调整基类如下:

public abstract class Vehicle : IComparable<Vehicle>, IComparable {
public Int16 AccidentID { get; set; }
public DateTime ProductionDate { get; set;}
public Vehicle(Int16 _ Vehicle ID,DateTime _ProductionDate)
{
    this.AccidentID = _ AccidentID;
    this.ProductionDate = _ProductionDate;
}
int IComparable.CompareTo(object other) {
    return CompareTo((Vehicle)other);
}
public int CompareTo(Vehicle other){
    return this.ProductionDate.CompareTo(other.ProductionDate);
}
public Vehicle()
{}

}

所以我改了:

public Int16 VehicleID;
public DateTime ProductionDate;

至:

public Int16 AccidentID { get; set; }
public DateTime ProductionDate { get; set;}

BTW:基类中有VehicleID字段,但在构造函数中,您将值设置为AccidentiID,而不是VehicleID。我认为这只是描述中的一个拼写错误,对吧?所以我使用了AccidentID作为属性名称,所以请检查它是否正确。