在实体框架如何设置默认值 [英] How can I set default values in Entity Framework

查看:149
本文介绍了在实体框架如何设置默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表在我的数据库52列,我想编写一个函数,以该表中创建行。
就我而言,我不希望使用的所有列在该表中,所以我创造了我的模型是这样的。

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

有没有办法通过实体框架,而无需编写所有在我的模型领域和做只发送这些数据并设置所有其他不可为空领域的一些默认数据(字符串,对于小数0.0等)手动?

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?

推荐答案

在没有纳入表列模型中的话,就不会被映射,它会被完全忽略所有生成的SQL。

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.

这篇关于在实体框架如何设置默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆