实现 SQL 的“不存在”在 Silverlight RIA 服务中

发布于 2024-12-23 04:06:05 字数 1773 浏览 5 评论 0原文

我正在尝试将 Silverlight 与 RIA 服务结合使用。 CRUD 操作非常简单。但我坚持“不存在”的情况。

我有两个实体 Person(personID,personName)和 Car(carIDpersonID)。有些人有车,但只有一辆车。

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里人 2024-12-30 04:06:05

如果“有人有一辆车,但只有一辆车” - 那么你就得到了错误的数据库架构。为什么不将可为 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 related Car.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文