2017-02-21 51 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)列会为你工作