2017-02-21 78 views
0

因此,當我保存更改時,我的代碼似乎正在破壞。我無法更新我的本地數據庫中的表。請尋求幫助。我很抱歉,如果這不僅僅是被問到我的問題。我不太瞭解linq是如何工作的以及爲了執行基本的SQL命令我需要做什麼。更新聲明使用Linq的本地數據庫

private void addUser() 
    { 
     try 
     { 

      var t = new tUser 
      { 
       Username = "test", 
       Password = "Password", 
       Email = "[email protected]", 
       Approved = true, 
       UserRoleID = 1, 
       First_Name = "test", 
       Last_Name = "test", 

      }; 
      this.db.AddTotUsers(t); 
      this.db.SaveChanges(); 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
    } 

這裏是TUSER生成的類

using System; 
    using System.ComponentModel; 
    using System.Data.EntityClient; 
    using System.Data.Objects; 
    using System.Data.Objects.DataClasses; 
    using System.Linq; 
    using System.Runtime.Serialization; 
    using System.Xml.Serialization; 

    [assembly: EdmSchemaAttribute()] 
    namespace AVOSoftware 
    { 
#region Contexts 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
public partial class Database1Entities : ObjectContext 
{ 
    #region Constructors 

    /// <summary> 
    /// Initializes a new Database1Entities object using the connection string found in the 'Database1Entities' section of the application configuration file. 
    /// </summary> 
    public Database1Entities() : base("name=Database1Entities", "Database1Entities") 
    { 
     this.ContextOptions.LazyLoadingEnabled = true; 
     OnContextCreated(); 
    } 

    /// <summary> 
    /// Initialize a new Database1Entities object. 
    /// </summary> 
    public Database1Entities(string connectionString) : base(connectionString, "Database1Entities") 
    { 
     this.ContextOptions.LazyLoadingEnabled = true; 
     OnContextCreated(); 
    } 

    /// <summary> 
    /// Initialize a new Database1Entities object. 
    /// </summary> 
    public Database1Entities(EntityConnection connection) : base(connection, "Database1Entities") 
    { 
     this.ContextOptions.LazyLoadingEnabled = true; 
     OnContextCreated(); 
    } 

    #endregion 

    #region Partial Methods 

    partial void OnContextCreated(); 

    #endregion 

    #region ObjectSet Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    public ObjectSet<tUser> tUsers 
    { 
     get 
     { 
      if ((_tUsers == null)) 
      { 
       _tUsers = base.CreateObjectSet<tUser>("tUsers"); 
      } 
      return _tUsers; 
     } 
    } 
    private ObjectSet<tUser> _tUsers; 

    #endregion 

    #region AddTo Methods 

    /// <summary> 
    /// Deprecated Method for adding a new object to the tUsers EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead. 
    /// </summary> 
    public void AddTotUsers(tUser tUser) 
    { 
     base.AddObject("tUsers", tUser); 
    } 

    #endregion 

} 

#endregion 

#region Entities 

/// <summary> 
/// No Metadata Documentation available. 
/// </summary> 
[EdmEntityTypeAttribute(NamespaceName="Database1Model", Name="tUser")] 
[Serializable()] 
[DataContractAttribute(IsReference=true)] 
public partial class tUser : EntityObject 
{ 
    #region Factory Method 

    /// <summary> 
    /// Create a new tUser object. 
    /// </summary> 
    /// <param name="userID">Initial value of the UserID property.</param> 
    /// <param name="username">Initial value of the Username property.</param> 
    /// <param name="password">Initial value of the Password property.</param> 
    /// <param name="userRoleID">Initial value of the UserRoleID property.</param> 
    /// <param name="first_Name">Initial value of the First_Name property.</param> 
    /// <param name="last_Name">Initial value of the Last_Name property.</param> 
    /// <param name="approved">Initial value of the Approved property.</param> 
    /// <param name="email">Initial value of the Email property.</param> 
    public static tUser CreatetUser(global::System.Int64 userID, global::System.String username, global::System.String password, global::System.Int32 userRoleID, global::System.String first_Name, global::System.String last_Name, global::System.Boolean approved, global::System.String email) 
    { 
     tUser tUser = new tUser(); 
     tUser.UserID = userID; 
     tUser.Username = username; 
     tUser.Password = password; 
     tUser.UserRoleID = userRoleID; 
     tUser.First_Name = first_Name; 
     tUser.Last_Name = last_Name; 
     tUser.Approved = approved; 
     tUser.Email = email; 
     return tUser; 
    } 

    #endregion 

    #region Primitive Properties 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int64 UserID 
    { 
     get 
     { 
      return _UserID; 
     } 
     set 
     { 
      if (_UserID != value) 
      { 
       OnUserIDChanging(value); 
       ReportPropertyChanging("UserID"); 
       _UserID = StructuralObject.SetValidValue(value); 
       ReportPropertyChanged("UserID"); 
       OnUserIDChanged(); 
      } 
     } 
    } 
    private global::System.Int64 _UserID; 
    partial void OnUserIDChanging(global::System.Int64 value); 
    partial void OnUserIDChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Username 
    { 
     get 
     { 
      return _Username; 
     } 
     set 
     { 
      OnUsernameChanging(value); 
      ReportPropertyChanging("Username"); 
      _Username = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("Username"); 
      OnUsernameChanged(); 
     } 
    } 
    private global::System.String _Username; 
    partial void OnUsernameChanging(global::System.String value); 
    partial void OnUsernameChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Password 
    { 
     get 
     { 
      return _Password; 
     } 
     set 
     { 
      OnPasswordChanging(value); 
      ReportPropertyChanging("Password"); 
      _Password = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("Password"); 
      OnPasswordChanged(); 
     } 
    } 
    private global::System.String _Password; 
    partial void OnPasswordChanging(global::System.String value); 
    partial void OnPasswordChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Int32 UserRoleID 
    { 
     get 
     { 
      return _UserRoleID; 
     } 
     set 
     { 
      OnUserRoleIDChanging(value); 
      ReportPropertyChanging("UserRoleID"); 
      _UserRoleID = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("UserRoleID"); 
      OnUserRoleIDChanged(); 
     } 
    } 
    private global::System.Int32 _UserRoleID; 
    partial void OnUserRoleIDChanging(global::System.Int32 value); 
    partial void OnUserRoleIDChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String First_Name 
    { 
     get 
     { 
      return _First_Name; 
     } 
     set 
     { 
      OnFirst_NameChanging(value); 
      ReportPropertyChanging("First_Name"); 
      _First_Name = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("First_Name"); 
      OnFirst_NameChanged(); 
     } 
    } 
    private global::System.String _First_Name; 
    partial void OnFirst_NameChanging(global::System.String value); 
    partial void OnFirst_NameChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Last_Name 
    { 
     get 
     { 
      return _Last_Name; 
     } 
     set 
     { 
      OnLast_NameChanging(value); 
      ReportPropertyChanging("Last_Name"); 
      _Last_Name = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("Last_Name"); 
      OnLast_NameChanged(); 
     } 
    } 
    private global::System.String _Last_Name; 
    partial void OnLast_NameChanging(global::System.String value); 
    partial void OnLast_NameChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.Boolean Approved 
    { 
     get 
     { 
      return _Approved; 
     } 
     set 
     { 
      OnApprovedChanging(value); 
      ReportPropertyChanging("Approved"); 
      _Approved = StructuralObject.SetValidValue(value); 
      ReportPropertyChanged("Approved"); 
      OnApprovedChanged(); 
     } 
    } 
    private global::System.Boolean _Approved; 
    partial void OnApprovedChanging(global::System.Boolean value); 
    partial void OnApprovedChanged(); 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)] 
    [DataMemberAttribute()] 
    public global::System.String Email 
    { 
     get 
     { 
      return _Email; 
     } 
     set 
     { 
      OnEmailChanging(value); 
      ReportPropertyChanging("Email"); 
      _Email = StructuralObject.SetValidValue(value, false); 
      ReportPropertyChanged("Email"); 
      OnEmailChanged(); 
     } 
    } 
    private global::System.String _Email; 
    partial void OnEmailChanging(global::System.String value); 
    partial void OnEmailChanged(); 

    #endregion 


} 

#endregion 

}

這裏是我得到

「消息」 服務器生成的密鑰和服務器生成的值異常SQL Server Compact不支持「字符串 」

+0

請分享'tUser'模型了。 – mindOfAi

+0

對不起,這個模型是什麼意思? – Dalton

+0

我在說'tUser'類。 – mindOfAi

回答

0

在我看來,像SQL Server Compact Edition不支持自動生成的密鑰。您需要自己增加UserId並提供該值或獲得更多功能豐富的SQL Server版本。我很確定SQL Server Express可以做到這一點。

0

您使用的是很舊版本的實體框架,升級到5或更高版本和SQL Server Compact服務器生成的密鑰(IDENTITY)列會爲你工作