1

I'm using Entity Framework with ASP.NET MVC 3 and it kindly creates the DB tables for my models for me. That's nice, but there is a simple thing I can't figure out: how do I make some fields NOT NULL in the database without using the [Required] attributes. These are fields that the server must set a value for before saving to the DB, but the user will not need to enter them, hence they are not "required" from a validation point of view.

2 Answers 2

2

That has nothing to do with your entities. If your entities must be not null they should be required for EF. If requirements for UI are different you need separate view model to represent correct validation rules for UI. Your business logic will be responsible to convert view model to entity and fill additional fields.

Sign up to request clarification or add additional context in comments.

Comments

1

Select the property in the EF design view, now you can change several options in the property pane, including NOT NULL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.