我绝对是使用 RIA 开发 Silverlight 的初学者
我按照教程从 silverlight.net 找到了 WCF RIA 服务示例
我在尝试添加新员工时遇到错误
它将弹出一个带有消息 VS JIT Debugger 的窗口
代码:4004
类别:托管运行时错误
消息:System.ServiceModel.DomainServices.Client.DomainException:提交更改类型的域上下文时发生错误
我正在使用 silverlight 4 和 AdventureWork2008Entities 做这个教程,下面是应用程序中的代码
<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();
}
}
员工列表.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);
}
}
组织服务