SQL Server CE - 继承映射 - 列不能包含空值异常

时间:2014-09-16 14:31:04

标签: linq sql-server-ce

我正在设计一个简单的数据库。两个表非常相似所以我使用了继承映射技术。

[Table]
[InheritanceMapping(Code = "P", Type = typeof(ATable),
        IsDefault = true)]
[InheritanceMapping(Code = "E", Type = typeof(BTable))]
public class ATable: TableBase
{

        [Column(IsDiscriminator = true)]
        public string DiscKey;

        private int _Id;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int Id
        {
            get { return _Id; }
            set
            {
                if (_Id != value)
                {
                    NotifyPropertyChanging("Id");
                    _Id = value;
                    NotifyPropertyChanged("Id");
                }
            }
        }

       ....

}

public class BTable : ATable
{

    private int _Property;
    [Column]
    public int Property
    {
        get { return _Property; }
        set
        {
            if (_Property!= value)
            {
                NotifyPropertyChanging("Property");
                _DeviceDetailsId = value;
                NotifyPropertyChanged("Property");
            }
        }
    }

   ...
}

//DBDATACONTEXT

public class DBDataContext : DataContext
    {
        public DBDataContext(string connectionString) : base(connectionString) {}
        public Table<ATable> As;
    }

当我尝试:

var db = DBDataContext("Data Source=isostore:/xDB.sdf;Password='testpass';");

var atable = new ATable();

db.As.InsertOnSubmit(atable);

db.SubmitChanges();

我得到一个例外:该列不能包含空值。 [列名=属性,表名= ATable]

怎么了?!

1 个答案:

答案 0 :(得分:0)

表上的行将始终包含所有继承实体的所有属性...因此使用CanBeNull = true标记子列(属性)将解决问题。