我绝对是初学者用RIA开发Silverlight
我按照教程
从silverlight.net找到了WCF RIA Services样本我在尝试添加新员工时收到错误
它将弹出一个窗口,其中包含消息VS JIT Debugger
代码:4004
类别:托管运行时错误
消息:System.ServiceModel.DomainServices.Client.DomainException:在类型
的域上下文上提交更改时发生错误我正在使用带有AdventureWork2008Entities的silverlight 4执行本教程,以下是应用程序中的代码
<dataForm:DataForm x:Name="addEmployeeDataForm" AutoGenerateFields="False" AutoCommit="True" AutoEdit="True" CommandButtonsVisibility="None">
<dataForm:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
<dataForm:DataField Label="Business Entity ID">
<TextBox Text="{Binding BusinessEntityID, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="Login ID">
<TextBox Text="{Binding LoginID, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="National ID">
<TextBox Text="{Binding NationalIDNumber, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="Title">
<TextBox Text="{Binding JobTitle, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="Marital Status">
<TextBox Text="{Binding MaritalStatus, Mode=TwoWay}" />
</dataForm:DataField>
<dataForm:DataField Label="Gender">
<TextBox Text="{Binding Gender, Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True }" />
</dataForm:DataField>
<dataForm:DataField Label="Salaried">
<CheckBox IsChecked="{Binding SalariedFlag, Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True }" />
</dataForm:DataField>
<dataForm:DataField Label="Active">
<CheckBox IsChecked="{Binding CurrentFlag, Mode=TwoWay,NotifyOnValidationError=True, ValidatesOnExceptions=True }" />
</dataForm:DataField>
</StackPanel>
</DataTemplate>
</dataForm:DataForm.EditTemplate>
</dataForm:DataForm>
员工注册窗口.xaml
private void addNewEmployee_Click(object sender,RoutedEventArgs e) { EmployeeRegistrationWindow addEmp = new EmployeeRegistrationWindow(); addEmp.Closed + = new EventHandler(addEmp_Closed); addEmp.Show(); }
private void addEmp_Closed(object sender, EventArgs e)
{
EmployeeRegistrationWindow emp = (EmployeeRegistrationWindow) sender;
if (emp.NewEmployee != null)
{
OrganizationContext _organizationContext = (OrganizationContext) (employeeDataSource2.DomainContext);
_organizationContext.Employees.Add(emp.NewEmployee);
employeeDataSource2.SubmitChanges();
}
}
员工List.xaml.cs
public void InsertEmployee(Employee employee)
{
employee.HireDate = DateTime.Now;
employee.ModifiedDate = DateTime.Now;
employee.VacationHours = 100;
employee.SickLeaveHours = 0;
employee.rowguid = Guid.NewGuid();
employee.BirthDate = new DateTime(1967, 3, 18);
if ((employee.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(employee, EntityState.Added);
}
else
{
this.ObjectContext.Employees.AddObject(employee);
}
}
OrganizationService
答案 0 :(得分:2)
我注意到没有人回复过这篇文章,但是我想在研究完全相同的问题时添加一些我遇到过的信息,万一它可以帮助某人,如果不是原始海报的话。
我假设您正在使用AdventureWorks2008R2数据库?如果没有,请忽略这篇文章的其余部分!
自生成Silverlight演练以来,一些列名显然已发生变化。在Employee表中,EmployeeID现在是BusinessEntityID,Title现在是JobTitle。如果你修复这些引用,你将得到一个不同的错误,这是由......
HumanResources.Employee表和Person.Person表之间存在外键关系 - 如果您要添加的Employee在Person表中没有相应的记录,则Insert将失败。因此,插入员工的代码应首先将记录插入Person.BusinessEntity表并检索生成的BusinessEntityID值。然后,应使用此BusinessEntityID值将相关数据插入Person.Person和HumanResources.Employee表。
至少,它看起来对我来说。希望这有助于某人。
_Ade