在插入实体框架中排除模型的属性 [英] Exclude property of model in insert Entity Framework

查看:71
本文介绍了在插入实体框架中排除模型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get an error when inserting data into my database. My database columns are:

username, password 

,然后我在模型中添加了 confirmpassword 来对其进行扩展.

and I added confirmpassword to the model to extend it.

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

How do I get rid of the confirmpassword to insert the username and password while using the model with confirmpassword property?

我的代码:

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; }
}

推荐答案

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

I think what you need to do is add a viewmodel for the page.First of all you will have model that is generated from entity framework which might look as below.

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

    public string username {get;set;}

    public string password {get;set;}
}

因此,现在为您的View创建一个viewModel.它可能如下所示.

So now create a viewModel for your View .This might look as below.

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; }
}

,现在在您的控制器操作方法中.您可以执行以下操作.

and now in your controller action method. you can do as below.

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

希望有帮助!

这篇关于在插入实体框架中排除模型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆