wpf app을 작성하고 있습니다. 다른 테이블에 해당하는 두 개의 엔티티 클래스가있는 로컬 데이터베이스 (sqlce)가 있습니다. 첫 번째 클래스는 Account이고 두 번째 클래스는 Movements입니다. 두 테이블간에 일대 다 관계가 있습니다. 계정에 더 많은 움직임이있을 수 있습니다. 다음은 계정 클래스입니다 :linq2sql 데이터베이스의 데이터를 일대 다 관계에 삽입하십시오.
[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 속성은 두 테이블을 연결하는 정의합니다. 그 후 나는 데이터 컨텍스트 클래스를 작성 : mainclass 나는 데이터베이스를 생성,하지만 난 계정 개체를 채울 때, 나는 SQL 예외가에서
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) { }
}
! InsertOnSubmit (Account a)를 문제없이 호출하는 데이터를 삽입 할 수 있지만 SubmitChanges()를 호출하면 프로그램이 중지되고 "열에 Null 값 [Column name = IDA, Table name = Account]"을 포함 할 수 없다는 메시지가 표시됩니다.
누구든지 나를 도와 줄 수 있습니까?
당신은 "나는 SQL 예외가 발생했습니다!"라고 말합니다. - 그것은 무엇입니까? 그들은 대개 매우 설명 적이기 때문입니다. –
Entity Framework를 사용할 수있을 때 왜 모든 것을 손으로 코딩하고 있습니까? –
wpf로 작성하고 있지만 wp7 앱을 만들고 싶습니다. –