在视图上Linq to SQL DeleteAllOnSubmit

时间:2011-10-25 11:25:35

标签: c# sql-server linq linq-to-sql

我的数据库中有一个视图。视图的实体具有IsPrimaryKey = true的主键标记。

当我运行db.MyEntity.DeleteAllOnSubmit(items);

我看到实体被标记为删除,但是在调用db.SubmitChanges();时没有生成SQL。

我使用SQL分析器,并且没有为删除生成或执行sql。 有什么建议??

视图的DBML条目:

[global::System.Data.Linq.Mapping.TableAttribute(Name="MyView")]
    public partial class Entity4ShowEntity1 : INotifyPropertyChanging, INotifyPropertyChanged
    {

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

        private string _Unid;

        private string _Col1;

        private string _Col2;

        private string _Col3;

        private string _Col4;

        private System.Nullable<System.DateTime> _LastUpdate;

        private string _Col5;

        private string _Col6;

        private System.Nullable<bool> _IsActive;

        private System.Nullable<bool> _IsDirty;

        private EntityRef<Entity1> _Entity1;

        private EntityRef<Entity2> _Entity2;

        private EntityRef<Entity3> _Entity3;

        private EntityRef<Entity4> _Entity4;

    #region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnUnidChanging(string value);
    partial void OnUnidChanged();
    partial void OnEntity4UnidChanging(string value);
    partial void OnEntity4UnidChanged();
    partial void OnEntity3UnidChanging(string value);
    partial void OnEntity3UnidChanged();
    partial void OnEntity2UnidChanging(string value);
    partial void OnEntity2UnidChanged();
    partial void OnEntity1UnidChanging(string value);
    partial void OnEntity1UnidChanged();
    partial void OnLastUpdateChanging(System.Nullable<System.DateTime> value);
    partial void OnLastUpdateChanged();
    partial void OnUserUnidChanging(string value);
    partial void OnUserUnidChanged();
    partial void OnRemarksChanging(string value);
    partial void OnRemarksChanged();
    partial void OnIsActiveChanging(System.Nullable<bool> value);
    partial void OnIsActiveChanged();
    partial void OnIsDirtyChanging(System.Nullable<bool> value);
    partial void OnIsDirtyChanged();
    #endregion

        public Entity4ShowEntity1()
        {
            this._Entity1 = default(EntityRef<Entity1>);
            this._Entity2 = default(EntityRef<Entity2>);
            this._Entity3 = default(EntityRef<Entity3>);
            this._Entity4 = default(EntityRef<Entity4>);
            OnCreated();
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Unid", DbType="NVarChar(55) NOT NULL", CanBeNull=false, IsPrimaryKey=true)]
        public string Unid
        {
            get
            {
                return this._Unid;
            }
            set
            {
                if ((this._Unid != value))
                {
                    this.OnUnidChanging(value);
                    this.SendPropertyChanging();
                    this._Unid = value;
                    this.SendPropertyChanged("Unid");
                    this.OnUnidChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col1", DbType="NVarChar(55) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
        public string Entity4Unid
        {
            get
            {
                return this._Col1;
            }
            set
            {
                if ((this._Col1 != value))
                {
                    if (this._Entity4.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnEntity4UnidChanging(value);
                    this.SendPropertyChanging();
                    this._Col1 = value;
                    this.SendPropertyChanged("Entity4Unid");
                    this.OnEntity4UnidChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col2", DbType="NVarChar(55) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
        public string Entity3Unid
        {
            get
            {
                return this._Col2;
            }
            set
            {
                if ((this._Col2 != value))
                {
                    if (this._Entity3.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnEntity3UnidChanging(value);
                    this.SendPropertyChanging();
                    this._Col2 = value;
                    this.SendPropertyChanged("Entity3Unid");
                    this.OnEntity3UnidChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col3", DbType="NVarChar(55) NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
        public string Entity2Unid
        {
            get
            {
                return this._Col3;
            }
            set
            {
                if ((this._Col3 != value))
                {
                    if (this._Entity2.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnEntity2UnidChanging(value);
                    this.SendPropertyChanging();
                    this._Col3 = value;
                    this.SendPropertyChanged("Entity2Unid");
                    this.OnEntity2UnidChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col4", DbType="NVarChar(55)", UpdateCheck=UpdateCheck.Never)]
        public string Entity1Unid
        {
            get
            {
                return this._Col4;
            }
            set
            {
                if ((this._Col4 != value))
                {
                    if (this._Entity1.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnEntity1UnidChanging(value);
                    this.SendPropertyChanging();
                    this._Col4 = value;
                    this.SendPropertyChanged("Entity1Unid");
                    this.OnEntity1UnidChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_LastUpdate", DbType="DateTime", UpdateCheck=UpdateCheck.Never)]
        public System.Nullable<System.DateTime> LastUpdate
        {
            get
            {
                return this._LastUpdate;
            }
            set
            {
                if ((this._LastUpdate != value))
                {
                    this.OnLastUpdateChanging(value);
                    this.SendPropertyChanging();
                    this._LastUpdate = value;
                    this.SendPropertyChanged("LastUpdate");
                    this.OnLastUpdateChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col5", DbType="NVarChar(55)", UpdateCheck=UpdateCheck.Never)]
        public string UserUnid
        {
            get
            {
                return this._Col5;
            }
            set
            {
                if ((this._Col5 != value))
                {
                    this.OnUserUnidChanging(value);
                    this.SendPropertyChanging();
                    this._Col5 = value;
                    this.SendPropertyChanged("UserUnid");
                    this.OnUserUnidChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Col6", DbType="NVarChar(255)", UpdateCheck=UpdateCheck.Never)]
        public string Remarks
        {
            get
            {
                return this._Col6;
            }
            set
            {
                if ((this._Col6 != value))
                {
                    this.OnRemarksChanging(value);
                    this.SendPropertyChanging();
                    this._Col6 = value;
                    this.SendPropertyChanged("Remarks");
                    this.OnRemarksChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsActive", DbType="Bit", UpdateCheck=UpdateCheck.Never)]
        public System.Nullable<bool> IsActive
        {
            get
            {
                return this._IsActive;
            }
            set
            {
                if ((this._IsActive != value))
                {
                    this.OnIsActiveChanging(value);
                    this.SendPropertyChanging();
                    this._IsActive = value;
                    this.SendPropertyChanged("IsActive");
                    this.OnIsActiveChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsDirty", DbType="Bit", UpdateCheck=UpdateCheck.Never)]
        public System.Nullable<bool> IsDirty
        {
            get
            {
                return this._IsDirty;
            }
            set
            {
                if ((this._IsDirty != value))
                {
                    this.OnIsDirtyChanging(value);
                    this.SendPropertyChanging();
                    this._IsDirty = value;
                    this.SendPropertyChanged("IsDirty");
                    this.OnIsDirtyChanged();
                }
            }
        }

        [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Entity1_Entity4ShowEntity1", Storage="_Entity1", ThisKey="Entity1Unid", OtherKey="Unid", IsForeignKey=true)]
        public Entity1 Entity1
        {
            get
            {
                return this._Entity1.Entity;
            }
            set
            {
                Entity1 previousValue = this._Entity1.Entity;
                if (((previousValue != value) 
                            || (this._Entity1.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Entity1.Entity = null;
                        previousValue.MyView.Remove(this);
                    }
                    this._Entity1.Entity = value;
                    if ((value != null))
                    {
                        value.MyView.Add(this);
                        this._Col4 = value.Unid;
                    }
                    else
                    {
                        this._Col4 = default(string);
                    }
                    this.SendPropertyChanged("Entity1");
                }
            }
        }

        [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Entity2_Entity4ShowEntity1", Storage="_Entity2", ThisKey="Entity2Unid", OtherKey="Unid", IsForeignKey=true)]
        public Entity2 Entity2
        {
            get
            {
                return this._Entity2.Entity;
            }
            set
            {
                Entity2 previousValue = this._Entity2.Entity;
                if (((previousValue != value) 
                            || (this._Entity2.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Entity2.Entity = null;
                        previousValue.MyView.Remove(this);
                    }
                    this._Entity2.Entity = value;
                    if ((value != null))
                    {
                        value.MyView.Add(this);
                        this._Col3 = value.Unid;
                    }
                    else
                    {
                        this._Col3 = default(string);
                    }
                    this.SendPropertyChanged("Entity2");
                }
            }
        }

        [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Entity3_Entity4ShowEntity1", Storage="_Entity3", ThisKey="Entity3Unid", OtherKey="Unid", IsForeignKey=true)]
        public Entity3 Entity3
        {
            get
            {
                return this._Entity3.Entity;
            }
            set
            {
                Entity3 previousValue = this._Entity3.Entity;
                if (((previousValue != value) 
                            || (this._Entity3.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Entity3.Entity = null;
                        previousValue.MyView.Remove(this);
                    }
                    this._Entity3.Entity = value;
                    if ((value != null))
                    {
                        value.MyView.Add(this);
                        this._Col2 = value.Unid;
                    }
                    else
                    {
                        this._Col2 = default(string);
                    }
                    this.SendPropertyChanged("Entity3");
                }
            }
        }

        [global::System.Data.Linq.Mapping.AssociationAttribute(Name="Entity4_Entity4ShowEntity1", Storage="_Entity4", ThisKey="Entity4Unid", OtherKey="Unid", IsForeignKey=true)]
        public Entity4 Entity4
        {
            get
            {
                return this._Entity4.Entity;
            }
            set
            {
                Entity4 previousValue = this._Entity4.Entity;
                if (((previousValue != value) 
                            || (this._Entity4.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Entity4.Entity = null;
                        previousValue.MyView.Remove(this);
                    }
                    this._Entity4.Entity = value;
                    if ((value != null))
                    {
                        value.MyView.Add(this);
                        this._Col1 = value.Unid;
                    }
                    else
                    {
                        this._Col1 = default(string);
                    }
                    this.SendPropertyChanged("Entity4");
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

确保db.ObjectTrackingEnabled = true;。默认情况下,它应设置为true,因此您可以手动将其更改为false。当您需要DataContext的只读(SELECT)模式时,这是一个很好的性能提升。但是你需要执行DELETE命令,所以它必须是真的。