0

I have some columns in table Tbl_User, but I want to insert data into two of them - not all - and I don't know how can write this code.

I created a repository and this code save all of data to table:

public bool InsertUser(Tbl_User t)
{
    db.Tbl_User.Add(t);
    return Convert.ToBoolean(db.SaveChanges());
}

but I want to save just in UserName and Password columns.

I created a viewmodel like this, but I don't know what shall I do?

   public int ID { get; set; }
   public string UserName { get; set; }
   public string Password { get; set; }
sepide725
  • 13
  • 4
  • 3
    *"but i want to save ... Password"*. **don't**. Never store plain text passwords in a database; they should always be salted and hashed. – Thom A Sep 20 '19 at 17:55
  • [How to hash a password?](https://stackoverflow.com/a/10402129/6311045) and [Passwrd hashing in C#](https://janaks.com.np/password-hashing-in-csharp/) –  Sep 21 '19 at 12:19
  • just search on c# password hash – Mark Schultheiss Sep 21 '19 at 12:19

1 Answers1

0

You can tag the property you do not want stored with [NotMapped]

   [NotMapped]
   public int ID { get; set; }
   public string UserName { get; set; }
   public string Password { get; set; }

Docs: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.schema.notmappedattribute?view=netframework-4.8

Also as Larnu pointed out saving a the password like this is NOT advisable.

Stephen McDowell
  • 839
  • 9
  • 21