使用Linq更新语句本地数据库

时间:2017-02-21 02:13:21

标签: c# linq

因此,当我保存更改时,我的代码似乎正在破坏。我无法更新本地数据库中的表。请寻求帮助。如果这不仅仅是对我的要求,我会道歉。我不太了解linq是如何工作的以及我需要做什么才能执行基本的SQL命令。

    private void addUser()
    {
        try
        {

            var t = new tUser
            {
                Username = "test",
                Password = "Password",
                Email = "test@test.com",
                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

}

这是我得到的例外

&#34;消息&#34; SQL Server Compact不支持服务器生成的密钥和服务器生成的值。&#34;串 &#34;

2 个答案:

答案 0 :(得分:0)

在我看来,SQL Server Compact版本不支持自动生成的密钥。您需要自己增加{{1}}并提供值或获得功能更丰富的SQL Server版本。我非常确定SQL Server Express可以做到这一点。

答案 1 :(得分:0)

您正在使用非常旧版本的Entity Framework,升级到版本5或更高版本,SQL Server Compact服务器生成的密钥(IDENTITY)列将适用于您