问题
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