이제 데이터베이스를 만들었습니다. SP1의 ADO.NET 엔티티 프레임 워크는 다음과 같이 엔터티 모델을 생성 한 2008Linq to Entities - 참조 EntityKey
비주얼 스튜디오 : http://img835.imageshack.us/img835/1810/carease15test1ef.png
나는 다음과 같은 방법을 만들었습니다
private void buttonAddPatient_Click(object sender, EventArgs e)
{
using (carease15test1Entities context = new carease15test1Entities())
{
long bnumber = Convert.ToInt32(textBoxToBed.Text);
long rnumber = Convert.ToInt32(textBoxOfRoom.Text);
long bid = (from b in context.bed
where b.bnumber == bnumber
select b.bid).First();
long rid = (from r in context.room
where r.rnumber == rnumber
select r.rid).First();
// Create a new bed, and input its details.
patient p = new patient();
p.pname = textBoxPatient.Text;
p.Bed_bid = bid;
p.bedReference.EntityKey = new EntityKey("carease15test1Entities.bed", "bid", bid);
p.Bed_Room_rid = rid;
p.bed.roomReference.EntityKey = new EntityKey("carease15test1Entities.room", "rid", rid);
context.AddTopatient(p);
try
{
context.SaveChanges();
}
catch (OptimisticConcurrencyException ex)
{
// Resolve the concurrency conflict by refreshing the // object context before re-saving changes.
context.Refresh(System.Data.Objects.RefreshMode.ClientWins, p);
// Save changes.
context.SaveChanges();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
모든 제외하고 완벽하게 작동을
에 대한p.bed.roomReference.EntityKey = new EntityKey ("carease15test1Entities.room", "rid", rid);
그것은 말하는 NullRefenceException 밝혀 "개체 참조가 개체의 인스턴스로 설정되지 않았습니다를."
을 여기 내 프로젝트 파일입니다.
http://www.mediafire.com/?w3rjjbxbxw6wacv
는 사람이 나에게 어떤 도움을 줄 수 있을까?
정말 고마워요.
가능한 복제본 [.NET의 NullReferenceException은 무엇입니까?] (http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –