5

I'm just thinking about a foreign key attribute in Entity Framework (using code first approach). I want to ensure that this foreign key is always set, in other words: It should be required.

Is using the "Required"-attribute/data annotation a clean solution for this? Or should this data annotation be used for user input only?

4

1 回答 1

8

I don't think you need to have required attribute in the data annotation . If you have declared it as

public int ForeignKeyName{get;set;}

It will take as required by EF. And if it

 public int? ForeignKeyName{get;set;}

it will be taken as optional(nullable) by entity framework Conventions . I'm not sure what kind of framework you are using for the web(front end Ex: asp.net mvc). Depend on that you need to think about that input level.

于 2013-02-27T10:39:49.357 回答