4

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123

3 Answers3

4

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.

H H
  • 263,252
  • 30
  • 330
  • 514
  • Eh :( It will be useful to have a something like that in Entity Framework. Thanks. – Chuck Norris May 03 '12 at 08:14
  • @ChuckNorris: EF doesn't know about these columns so how should it fill them? Create stored procedure if you don't want to map those columns and use stored procedure for inserting your data. – Ladislav Mrnka May 03 '12 at 08:19
  • I've already thought about solving problem with stored procedures. I will go with that. Thanks for your help :) – Chuck Norris May 03 '12 at 08:22
0

If you set the values in the constructor you will have a default value through your code, but you could look into enabling migrations instead, this way you can set default values. look at this stackoverflow question

Community
  • 1
  • 1
Jim Wolff
  • 5,052
  • 5
  • 34
  • 44
  • This one required filling my model again. I don't just write somewhere that for nvarchar not nullable fields set empty string without mentioning that fields. – Chuck Norris May 03 '12 at 08:08
0

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.

Community
  • 1
  • 1
tne
  • 7,071
  • 2
  • 45
  • 68