Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set default values in Entity Framework

I've a table with 52 columns in my database and I want to write a function to create a row in that table.
In my case, I don't want to use all columns in that table, so I created my model like this.

[Table("CUST_MASTER")]
public class CustomerMaster
{
    [Key]
    [Column("CUSTOMER_ID")]
    public string Id { get; set; }

    [Column("CUSTOMER_NAME")]
    public string Name { get; set; }

    [Column("CUSTOMER_CITY")]
    public string City { get; set; }
 }

Is there any way to send only this data via Entity framework and set all other not nullable fields to some default data(for strings "", for decimals 0.0, etc.) without writing all that fields in my model and doing it manually?

like image 944
Chuck Norris Avatar asked Sep 05 '25 03:09

Chuck Norris


1 Answers

When you do not incorporate a Table-column in your model then it won't be mapped and it will be totally ignored by all generated SQL.

So the only option is to specify a default value in your Database.

like image 179
Henk Holterman Avatar answered Sep 07 '25 20:09

Henk Holterman