0

我正在写一个 wpf 应用程序。我有一个本地数据库(sqlCE),其中有两个实体类对应于不同的表。第一个类是 Account,第二个类是 Movements。两个表之间存在一对多的关系:一个帐户可以有更多的动作。这是帐户类:

[Table]
public class Account
{
    .....other private fields...
    private Int16 iDA;
    private EntitySet<Movement> movements;

    ...other properties with column attribute....

    //primary key
    [Column(IsPrimaryKey = true, Storage="iDA", IsDbGenerated = true, AutoSync = AutoSync.OnInsert, DbType = "smallint")]
    public Int16 IDA
    {
        get { return iDA; }
        private set { iDA = value; }
    }

    //association
    [Association(Storage = "movements", OtherKey = "IDA")]
    public EntitySet<Movement> Movements
    {
        get { return movements; }
        set { this.movements.Assign(value); }
    }       

    public Account()
    {
        this.movements = new EntitySet<Movement>();
    }
}

这是运动课:

[Table]
public class Movement
{
    ...other fields...
    private Int16 iDA;
    private int iDM;
    private EntityRef<Account> myAcc;

    //primary key
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert, DbType = "int NOT NULL IDENTITY", Storage = "iDM")]
    public int IDM
    {
        get { return iDM; }
        set { iDM = value; }
    }

    //property links the two tables
    [Column(DbType = "smallint", Storage="iDA", isPrimaryKey=true)]
    public Int16 IDA
    {
        get { return iDA; }
        set { iDA = value; }
    }

    //association
    [Association(Storage = "myAcc", ThisKey = "IDA")]
    public Account MyAccount
    {
        get { return this.myAcc.Entity; }
        set { this.myAcc.Entity = value; }
    }

    ......

    public Movement()
    {
        this.myAcc = new EntityRef<Account>();
    }

}

我定义 IDA 属性来链接这两个表。之后我编写 datacontext 类:

public class DataBase : DataContext
{

    public Table<Account> AccountTable
    {
        get { return this.GetTable<Account>(); }
    }
    public Table<Movement> MovementTable
    {
        get { return this.GetTable<Movement>(); }
    }

    public DataBase(string connection) : base(connection) { }
}

在主类中我创建了数据库,但是当我尝试使用帐户对象填充它时,我得到了一个 sql 异常!我可以毫无问题地插入调用 InsertOnSubmit(Account a) 的数据,但是当我调用 SubmitChanges() 时,程序停止并且异常显示“列不能包含空值。[列名 = IDA,表名 = 账户]”。

任何人都可以帮助我吗?

4

2 回答 2

0

我已经解决了我的问题,在两个类中更改了 Int IDA 属性并进行了一些调整。

于 2013-01-12T10:27:34.073 回答
0

尝试对 IDA 字段的 Column 属性使用DbType = "smallint not null identity"CanBeNull = false参数。

于 2013-01-11T06:22:00.613 回答