Entity Model - How to use Database's Default Column Values while inserting

吃可爱长大的小学妹 提交于 2019-12-12 02:03:49

问题


I am using SQLServer 2008, WebForms, C#, Frameowrk 3.5, Entity Framework 1 I generated entity model from DB.

My DB has various tables & lets take example of one table user It has fields id, name, email, created_on, is_active, is_on_leave default values for is_active and on_leave properties are default to 0 ( zero ) in db

But When I try to insert a record in this table using entity model, it saves NULL in these fields. How to avoid this? I don't want to set a value for them from my page and want to use the one mentioned in DB.

How can I achieve that?


回答1:


This columns can be nullable. Right click property in designer, choose properties and set StoreGeneratedPatern for property to computed in the property window. I found similar question: Set default value in EF designer datetime




回答2:


The is_active and is_on_leave columns in your database are nullable. If you make them non-nullable the default values should be used when you do an insert.

ALTER TABLE [user] ALTER COLUMN [is_active] DEFAULT(0) NOT NULL
ALTER TABLE [user] ALTER COLUMN [is_on_leave] DEFAULT(0) NOT NULL
GO


来源:https://stackoverflow.com/questions/6842088/entity-model-how-to-use-databases-default-column-values-while-inserting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!