实现 SQL 的“不存在”在 Silverlight RIA 服务中
我正在尝试将 Silverlight 与 RIA 服务结合使用。 CRUD 操作非常简单。但我坚持“不存在”的情况。
我有两个实体 Person(personID,personName)和 Car(carID,personID)。有些人有车,但只有一辆车。
internal sealed class Person
{
private PersonMetadata() { }
public int personID { get; set; }
public string personName { get; set; }
[Include]
public Car CarNavigation { get; set; }
}
internal sealed class Car
{
private CarMetadata() { }
public string carName { get; set; }
public int personID { get; set; }
}
public IQueryable<Person> GetPersonEager()
{
return this.ObjectContext.Person.Include("CarNavigation");
}
<sdk:DataGrid Name="PersonGrid" AutoGenerateColumns="False">
<sdk:DataGrid.Columns >
<sdk:DataGridTextColumn Binding="{Binding personID}" />
<sdk:DataGridTextColumn Binding="{Binding personName}" />
<sdk:DataGridTextColumn Binding="{Binding CarNavigation.carName}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
PersonGrid.ItemsSource = context.Person;
context.Load(context.GetPersonEager());
这个结构工作得很好,适合更新。我还可以更新汽车的名称。
但是,我还想列出没有汽车的人。我还希望能够向其中添加汽车。我尝试过:
public IQueryable<Person> GetPersonEagerWhoDoesNotHaveACar()
{
return this.ObjectContext.Person.Include("CarNavigation").Where( x => x.CarNavigation == null );
}
上面的方法返回没有汽车的人,但汽车实体是空的。我如何修改它以使其返回没有汽车的人并且汽车实体的密钥已填充。
I'm trying to use Silverlight with RIA services. CRUD operations are pretty easy. But I am stuck with the "not exists" case.
I have two entities Person (personID, personName) and Car(carID, personID). Some person have a car, but only one car.
internal sealed class Person
{
private PersonMetadata() { }
public int personID { get; set; }
public string personName { get; set; }
[Include]
public Car CarNavigation { get; set; }
}
internal sealed class Car
{
private CarMetadata() { }
public string carName { get; set; }
public int personID { get; set; }
}
public IQueryable<Person> GetPersonEager()
{
return this.ObjectContext.Person.Include("CarNavigation");
}
<sdk:DataGrid Name="PersonGrid" AutoGenerateColumns="False">
<sdk:DataGrid.Columns >
<sdk:DataGridTextColumn Binding="{Binding personID}" />
<sdk:DataGridTextColumn Binding="{Binding personName}" />
<sdk:DataGridTextColumn Binding="{Binding CarNavigation.carName}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
PersonGrid.ItemsSource = context.Person;
context.Load(context.GetPersonEager());
This structure works pretty well, for update. I also can update the car's name.
But, I also want to list the persons who does not have a car. And I also want to be able to add cars to them. I have tried:
public IQueryable<Person> GetPersonEagerWhoDoesNotHaveACar()
{
return this.ObjectContext.Person.Include("CarNavigation").Where( x => x.CarNavigation == null );
}
The method above returns me the persons without cars, but the Car entity is empty. How sould I modify this to make it return the persons without cars and the car entity's key is filled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“有人有一辆车,但只有一辆车” - 那么你就得到了错误的数据库架构。为什么不将可为 null 的 carId 放入
Person
中?在这种情况下,您可以轻松地包含和操作(添加、删除等)您想要的所有内容,无论是否有相关的Car
。注意:如果您在当前架构下需要这种方法 - 那么请澄清应该返回什么查询(对于未连接的项目,有车人员列表设置为 null)以及目的。
If "Some person have a car, but only one car" - then you've got wrong DB architecture. Why do not to place nullable carId into
Person
? In such case you'll easily include and manipulate(add, delete etc.) everything you want with or without relatedCar
.Note: if you steel need such approach with current architecture - then clarify what should return query(list of Persons with Car set to null for not connected items) and for what purpose.