3

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

[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 等),而无需在我的模型中写入所有这些字段并手动执行?

4

3 回答 3

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.

于 2012-05-03T08:09:19.020 回答
0

我认为这个旧建议就是你想要的。它明确提到了概念模型和存储模型之间缺乏映射。虽然不是一个非常流行/理解的想法。

更新: FWIW,表明在非 Code-First 场景中已经有可能。

于 2013-11-05T17:40:47.050 回答
0

如果您在构造函数中设置值,您将通过代码获得默认值,但您可以考虑启用迁移,这样您就可以设置默认值。看看这个stackoverflow问题

于 2012-05-03T08:05:53.783 回答