with ef 4.1 code first how can i create a nullable column

馋奶兔 提交于 2019-12-13 07:42:52

问题


I have the following POCO:

Public Class T1

<Required()>
<MaxLength(128)>
<Key(), Column(Order:=0)>
Property K1 As String

<Required()>
<MaxLength(128)>
<Key(), Column(Order:=1)>
Property K2 As String

<Required()>
Property C1 As String

Property C2 As String

end Class

I would expect C2 to be created as Nullable, but both C1 and C2 are Not Null. Adding

    <Required(AllowEmptyStrings:=True)>

Does not make a difference, as it seems the decoration is aimed at data validation, not DB creation.

So how do i get a nullable column with Code First?


回答1:


There is no way to say that string property is nullable by attributes because it is default behaviour if you don't mark property with RequiredAttribute. In case of fluent mapping you can describe it as:

context.Entity(Of T1)().Property(Function(t) t.C2).IsNullable(True)


来源:https://stackoverflow.com/questions/6011098/with-ef-4-1-code-first-how-can-i-create-a-nullable-column

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