2010-11-29 60 views
1

我正在使用「對象關係設計器」爲我的SQL數據庫表創建實體。爲什麼LINQ To SQL代理類有時會實現INotifyPropertyChanging和INotifyPropertyChanged

問題:爲什麼有些類實現INotifyPropertyChanging,INotifyPropertyChanged和其他不?

當我想要使用LINQ To SQL實體更新數據庫中的某些數據時出現問題,該實體不實現INotifyPropertyChanging,INotifyPropertyChanged

實體defeniion:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.user_to_user")] 
public partial class user_to_user 
{  
    private int _user_id1; 

    private int _user_id2; 

    private string _relation_type; 

    public user_to_user() 
    { 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_user_id1", DbType="Int NOT NULL")] 
    public int user_id1 
    { 
     get 
     { 
      return this._user_id1; 
     } 
     set 
     { 
      if ((this._user_id1 != value)) 
      { 
       this._user_id1 = value; 
      } 
     } 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_user_id2", DbType="Int NOT NULL")] 
    public int user_id2 
    { 
     get 
     { 
      return this._user_id2; 
     } 
     set 
     { 
      if ((this._user_id2 != value)) 
      { 
       this._user_id2 = value; 
      } 
     } 
    } 

    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_relation_type", DbType="VarChar(50) NOT NULL", CanBeNull=false)] 
    public string relation_type 
    { 
     get 
     { 
      return this._relation_type; 
     } 
     set 
     { 
      if ((this._relation_type != value)) 
      { 
       this._relation_type = value; 
      } 
     } 
    } 
} 

的DataContext:

private Table<user_to_user> friendsTable = new BSocialDBDataContext().GetTable<user_to_user>(); 

然後我嘗試:

public void FriendshipOffer(int visitor_id, int owner_id) 
    { 
     var relation = friendsTable.FirstOrDefault(u => (u.user_id2 == visitor_id && u.user_id1 == owner_id)); 
     relation.relation_type = "friend"; 

     friendsTable.Context.SubmitChanges(); 
    } 

回答

1

包含在您的LINQ語句返回的數據實體鍵?

+0

user_to_user是一個coccection表,它不能有主鍵。如果不在user_to_user表中包含主鍵,我可以使用LINQ to SQL進行更新嗎? – Turach 2010-11-29 23:57:32

相關問題