【问题标题】:How to insert in some special columns in a table in SQL Server from ASP.NET MVC?如何从 ASP.NET MVC 在 SQL Server 的表中插入一些特殊列?
【发布时间】:2019-09-20 17:52:37
【问题描述】:

我在表 Tbl_User 中有一些列,但我想将数据插入其中两个 - 不是全部 - 我不知道如何编写此代码。

我创建了一个存储库,这段代码将所有数据保存到表中:

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

但我只想保存在 UserNamePassword 列中。

我创建了一个这样的视图模型,但我不知道该怎么办?

   public int ID { get; set; }
   public string UserName { get; set; }
   public string Password { get; set; }

【问题讨论】:

标签: c# sql-server asp.net-mvc


【解决方案1】:

你可以用 [NotMapped] 标记你不想存储的属性

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

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

正如 Larnu 指出的那样,保存这样的密码是可取的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多