返回数据库中的外键 class

Returning the class which is a foreign key in the database

我想问这个问题,我试着搜索了一段时间没有具体答案。

我创建了一个数据库并使用 LINQ2SQL 自动生成所需的 classes。 我已将序列化模式设置为单向,以确保 classes 正在被序列化并生成数据成员。

现在,我想知道的是,如何将引用发送给其他 classes(已通过 LINQ2SQL 完成)。

F.x。我有一个名为 Scheduler 的 Class,它引用 Reservation 和 Seat,因为 Reservation 和 Seat 有外键。

您可以在此处查看 dbml: http://imgur.com/rR6OxDi dbml 文件。这是我们数据库的模型

您还可以看到,当我 运行 WCF 测试客户端时,它不会 return 座位和预订的对象。 http://imgur.com/brxNBz7

希望大家能帮忙

更新

这是 LINQ2SQL 提供的代码片段。 这是调度程序的字段

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Scheduler")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class Scheduler : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _SchID;

    private System.Nullable<System.DateTime> _Date;

    private System.Nullable<System.TimeSpan> _Starttime;

    private System.Nullable<int> _MovieID;

    private System.Nullable<int> _HallID;

    private EntitySet<Seat> _Seats;

    private EntitySet<Reservation> _Reservations;

    private EntityRef<Hall> _Hall;

    private EntityRef<Movie> _Movie;

    private bool serializing;

这里是引用预订和座位的代码片段:

[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Scheduler_Seat", Storage="_Seats", ThisKey="SchID", OtherKey="SchedulerID")]
    [global::System.Runtime.Serialization.DataMemberAttribute(Order=6, EmitDefaultValue=false)]
    public EntitySet<Seat> Seats
    {
        get
        {
            if ((this.serializing 
                        && (this._Seats.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return this._Seats;
        }
        set
        {
            this._Seats.Assign(value);
        }
    }

    [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Scheduler_Reservation", Storage="_Reservations", ThisKey="SchID", OtherKey="SchedulerID")]
    [global::System.Runtime.Serialization.DataMemberAttribute(Order=7, EmitDefaultValue=false)]
    public EntitySet<Reservation> Reservations
    {
        get
        {
            if ((this.serializing 
                        && (this._Reservations.HasLoadedOrAssignedValues == false)))
            {
                return null;
            }
            return this._Reservations;
        }
        set
        {
            this._Reservations.Assign(value);
        }
    }

更新 2 这是 LINQ2SQL 制作的 Reservation class: 这是字段:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Reservation")]
[global::System.Runtime.Serialization.DataContractAttribute()]
public partial class Reservation : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _ResID;

    private System.Nullable<int> _CustomerID;

    private System.Nullable<int> _SchedulerID;

    private string _Row;

    private string _Seat;

    private EntityRef<Customer> _Customer;

    private EntityRef<Scheduler> _Scheduler;

这是 class

Scheduler 参考部分
[global::System.Data.Linq.Mapping.AssociationAttribute(Name="Scheduler_Reservation", Storage="_Scheduler", ThisKey="SchedulerID", OtherKey="SchID", IsForeignKey=true, DeleteRule="SET DEFAULT")]
    public Scheduler Scheduler
    {
        get
        {
            return this._Scheduler.Entity;
        }
        set
        {
            Scheduler previousValue = this._Scheduler.Entity;
            if (((previousValue != value) 
                        || (this._Scheduler.HasLoadedOrAssignedValue == false)))
            {
                this.SendPropertyChanging();
                if ((previousValue != null))
                {
                    this._Scheduler.Entity = null;
                    previousValue.Reservations.Remove(this);
                }
                this._Scheduler.Entity = value;
                if ((value != null))
                {
                    value.Reservations.Add(this);
                    this._SchedulerID = value.SchID;
                }
                else
                {
                    this._SchedulerID = default(Nullable<int>);
                }
                this.SendPropertyChanged("Scheduler");
            }
        }
    }

所有这些事情都会导致我可以像这样获得对象:

            Scheduler[] schedulers = client.GetAllSchedulers();
            Reservation reservation = schedulers[0].Reservations.First();

但由于 WCF 未发送对象(您可以在图一中看到)而出现此错误。 这是哪个错误:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Sequence contains no elements

更新 3: 好的,看来它以某种方式起作用。 我只需要在 SchedulerReservation 之间进行连接。 此外,每当我调试代码时,我都可以看到变量在那里。 (由于我的声誉,我无法 post 链接)。

但是当您尝试在调试模式下查看结果时,有些人可能会认出以下内容:

"expanding the results view will enumerate the ienumerable c#"

每当我这样做时,它都有效,但如果我 运行 它处于发布模式,则无效。

看起来只有 object 类型(ReservationSeat)具有 null 值。

我猜您要么在复杂类型中缺少 DataContract/DataMember 属性,要么您可能需要包含 KnownTypeAttribute

如果你能提供一些代码就更容易判断了。

编辑

你后面说的是延迟加载。有关延迟加载与立即加载的更多信息,请参阅 this 博客。

当您在调试模式下展开 IEnumerable 时,会向 retrieve/load 对象发出请求。

您可能想要的是加载 ReservationSeat 对象以及 Scheduler 对象。类似于以下内容:

YourDatabaseContext database = new YourDatabaseContext ())
{
    DataLoadOptions options = new DataLoadOptions();
    options.LoadWith<Scheduler>(sch=> sch.Reservation);
    options.LoadWith<Scheduler>(sch=> sch.Seat);
    database.LoadOptions = options;
}

有关详细信息,请参阅 DataLoadOptions

如果你想了解延迟执行。有关详细信息,请参阅 this 文章。 引用文章:

By default LINQ uses deferred query execution. This means when you write a LINQ query it doesn't execute. LINQ queries execute when you 'touch' the query results. This means you can change the underlying collection and run the same query subsequent times in the same scope. Touching the data means accessing the results, for instance in a for loop or by using an aggregate operator like Average or AsParallel on the results.