0

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

username, password 

and I added confirmpassword to the model to extend it.

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

My code:

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();

Model

[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; }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Icer Exiomo
  • 49
  • 1
  • 9
  • 1
    why is the confirmpassword field part of your schema? Surely that's an app-only thing, you don't need to store it? Maybe you just need a view model for your app separate from the DB model, like the way most MVC apps are designed. – ADyson May 18 '18 at 15:41
  • i extend the model. let me edit my post, ill add my model. Please check it. Thank you – Icer Exiomo May 18 '18 at 15:42
  • 1
    I agree with @ADyson, it seems to be you need to split your Data and View models, then you can post a View model with the confirm password and use this to populate a data model with the correct data. – John May 18 '18 at 15:47
  • It also occurs to me that you used to be able to use the [NotMapped] data annotation over a property you wished to ignore but I am not sure this still works and I think the splitting out of models is a better idea. – John May 18 '18 at 15:49
  • "i extend the model"... I know you did, but why? You don't need to store that value in the database, it's just duplication. Learn about separating your database data from your view data using a ViewModel class. Then you can have different fields on the view to those in the database, or combine different database tables onto one view, etc etc. Much more flexible. – ADyson May 18 '18 at 16:02

2 Answers2

3

This SO has three different solutions that may help you:

How not persist property EF4 code first?

Summary:

First try the DataAnnotations approach:

Make sure you include the required library. Then apply the [Not Mapped] annotation to your field in the model.

using System.ComponentModel.DataAnnotations;

[NotMapped]
public string confirmPassword { get; set; }

If this doesn't do it, try to modify your OnModelBuilding method in your dbContext. There are two options in this block. The first is to use the Ignore method.

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

The second is to manually remap the model building and exclude your field.

Additional SO answers that may be useful:

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 on how to manually map properties to db fields:

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

Clean example of answer provided:

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

user7396598
  • 1,269
  • 9
  • 6
  • I tried it but not working. Im using ef6. I can solve it by making viewmodel, but I want to learn how to ignore the property. – Icer Exiomo May 18 '18 at 16:40
  • @Icer Exiomo There are three solutions here, you tried all three? None of them involve making an additional view model. Though, that is a fourth possible solution. – user7396598 May 18 '18 at 16:46
0

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

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

Hope it helps !

Yogesh
  • 153
  • 1
  • 12