Home > Enterprise >  Entity Framework Core - Exclude entity property from view, but include in database table
Entity Framework Core - Exclude entity property from view, but include in database table

Time:08-14

I am adding to an entity a property called DateCreated, which will be set programmatically. I want the update-database process to create a table column for it, as it does for all other properties. But I do not want it to create a date picker for it in the corresponding create view.

I believe the NotMapped attribute will exclude the property from the DB table completely

Example:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    //dont want this to show on Person create page, but should appear is DB table column
    public DateTime DateCreated { get; set; }
}

CodePudding user response:

Create a DTO for it and exclude the date property

public class PersonDTO
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

Now in your C# code you can simply convert it back to the person and set the date automatically.

Person p = dtoperson.Adapt<Person>(); //mapster example, you can use automapper optionally
p.DateCreated = DateTime.Now;

You can also set a default value to "CURRENT_TIMESTAMP" in your dbconfig, now when you create a new record, you shouldn't have to set it manually.

CodePudding user response:

You could try [ScaffoldColumn] attribute, to avoid automatic generation of field, but also [Bindable(false)], to make sure that model binding engine never binds property coming from the (malicious) client:

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

public class Person
{
    [HiddenInput]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [ScaffoldColumn(false)]
    [Bindable(false)]
    public DateTime DateCreated { get; set; }
}

Another, often used attribute for scaffolding is [HiddenInput], usually used for auto-generated Ids.

If you are sending Person as JSON to the client, than you would also have to add one of this two attributes, depending which library does JSON serialization.

  • [System.Text.Json.Serialization.JsonIgnore]
  • [Newtonsoft.Json.JsonIgnore]

on DateCreated property.

CodePudding user response:

With guidance from a similar question, this works for what I am after:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Required, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime DateCreated { get; set; }
}

//on migration
migrationBuilder.AlterColumn<DateTime>(name: "DateCreated", table: "Person", defaultValueSql: "GETDATE()");
    

This is working. DateCreated is not shown as a view component. DateCreated is shown in DB table, and is automatically set upon insertion.

  • Related