简体   繁体   中英

Entity Framework make a field required based on data

I am working on a C# Silverlight application that uses Entity Framework, I was wondering if there is a way to make a field required based on other pieces of data? To make this more complicated the application has 1 save button that lives outside of the various forms and is added and uses impersonation to keep all the save functions in 1 spot.

Now for example I have member type 1,2,3,4,5,6. If the type is 1-4 then the DOB is required and cannot be left null and will provide the error to support that. But for 5-6 it can be left null.

From what I can tell, the errors are being thrown by Entity itself when it is not given a null to insert into a not null.

Unfortunately, it is not possible to do what you want.

Think how the underlying schema works, it defines what must be true for every record of the table.

You'll have to enforce that requirement via the business logic of your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM