How can I set default values in Entity Framework

.net c# default-value entity-framework sql-server-2008

Question

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?

1
3
5/3/2012 8:10:01 AM

Accepted Answer

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.

4
5/3/2012 8:09:19 AM

Popular Answer

I think this old suggestion is what you want. It explicitly mentions the lack of mapping between the conceptual model and the storage model. Not a very popular/understood idea though.

Update: FWIW, this suggests that it is already possible in non-Code-First scenarios.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow