【问题标题】:Exclude property of model in insert Entity Framework在插入实体框架中排除模型的属性
【发布时间】:2018-10-29 02:27:11
【问题描述】:

将数据插入数据库时​​出现错误。我的数据库列是:

username, password 

我在模型中添加了confirmpassword 来扩展它。

如何在使用具有confirmpassword 属性的模型时摆脱confirmpassword 以插入用户名和密码?

我的代码:

BootstrapTrainingEntities db = new BootstrapTrainingEntities();

var u = new User();

u.username = user.username;
u.password = user.password;
// how to I remove or ignore the confirmPassword property when saving?

db.Users.Add(u);
db.SaveChanges();

型号

[MetadataType(typeof(metadataUser))]
public partial class User
{
    public string confirmPassword { get; set; }
}

public class metadataUser
{
    [Required(ErrorMessage = "Username is required", AllowEmptyStrings = false)]
    [Display(Name ="Username")]
    public string username { get; set; }

    [Required(ErrorMessage ="Password is required", AllowEmptyStrings = false)]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string password { get; set; }

    [Required(ErrorMessage ="Confirmation Password is required", AllowEmptyStrings = false)]
    [Display(Name ="Confiramation Password")]
    [DataType(DataType.Password)]
    [Compare("password",ErrorMessage = "Password does not match")]
    public string confirmPassword { get; set; }
}

【问题讨论】:

  • 为什么确认密码字段是架构的一部分?当然这是一个仅限应用程序的东西,你不需要存储它吗?也许您只需要一个独立于 DB 模型的应用程序视图模型,就像大多数 MVC 应用程序的设计方式一样。
  • 我扩展了模型。让我编辑我的帖子,生病添加我的模型。请检查一下。谢谢
  • 我同意@ADyson,看来您需要拆分数据和视图模型,然后您可以使用确认密码发布视图模型并使用它来填充正确的数据模型数据。
  • 我还想到您曾经能够在您希望忽略的属性上使用 [NotMapped] 数据注释,但我不确定这仍然有效,我认为拆分模型是一个更好的主意。
  • "我扩展了模型"...我知道你做到了,但为什么呢?您不需要将该值存储在数据库中,这只是重复。了解如何使用 ViewModel 类将数据库数据与视图数据分开。然后,您可以在视图上拥有与数据库中不同的字段,或者将不同的数据库表组合到一个视图中,等等。更加灵活。

标签: asp.net-mvc entity-framework-6


【解决方案1】:

我认为您需要为页面添加一个视图模型。首先,您将拥有从实体框架生成的模型,如下所示。

public class User
{
    public int id{get;set;}

    public string username {get;set;}

    public string password {get;set;}
}

现在为您的 View 创建一个 viewModel。这可能如下所示。

public class UserViewModel
{
   public int id{get;set;}

   [Required(ErrorMessage = "Username is required", AllowEmptyStrings = false)]
   [Display(Name ="Username")]
   public string username { get; set; }

   [Required(ErrorMessage ="Password is required", AllowEmptyStrings = false)]
   [Display(Name = "Password")]
   [DataType(DataType.Password)]
   public string password { get; set; }

   [Required(ErrorMessage ="Confirmation Password is required", AllowEmptyStrings = false)]
   [Display(Name ="Confiramation Password")]
   [DataType(DataType.Password)]
   [Compare("password",ErrorMessage = "Password does not match")]
   public string confirmPassword { get; set; }
}

现在在您的控制器操作方法中。您可以按照以下方式进行操作。

[HttpPost]
public ActionResult AddUser(UserViewModel model)
{
    User user=new User();
    user.username=model.username;
    user.password=model.password;
    db.User.Add(user);
}

希望对你有帮助!

【讨论】:

    【解决方案2】:

    此 SO 提供了三种不同的解决方案,可能会对您有所帮助:

    How not persist property EF4 code first?

    总结:

    首先尝试 DataAnnotations 方法:

    确保包含所需的库。然后将 [Not Mapped] 注释应用到模型中的字段。

    using System.ComponentModel.DataAnnotations;
    
    [NotMapped]
    public string confirmPassword { get; set; }
    

    如果不这样做,请尝试在您的 dbContext 中修改您的 OnModelBuilding 方法。此块中有两个选项。第一种是使用Ignore方法。

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<MetaDataUser>().Ignore(p => p.confirmPassword);
        }
    }
    

    第二种是手动重新映射模型构建并排除您的字段。

    其他可能有用的 SO 答案:

    Entity Framework Code First - How to ignore a column when saving

    Exclude a field/property from the database with Entity Framework 4 & Code-First

    关于如何手动将属性映射到数据库字段的 MS Doc:

    https://docs.microsoft.com/en-us/ef/core/modeling/relational/columns

    提供的答案的简洁示例:

    http://www.dotnetodyssey.com/2015/03/31/ignore-class-property-to-table-column-in-entity-framework-code-first/

    【讨论】:

    • 我试过了,但没有用。我正在使用 ef6。我可以通过制作viewmodel来解决它,但是我想学习如何忽略该属性。
    • @Icer Exiomo 这里有三种解决方案,你都试过了吗?它们都不涉及制作额外的视图模型。不过,这是第四种可能的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 1970-01-01
    • 2014-12-05
    • 2015-03-21
    • 2017-10-05
    相关资源
    最近更新 更多