与ef 4.1代码首先我如何创建一个可空的列 [英] with ef 4.1 code first how can i create a nullable column

查看:141
本文介绍了与ef 4.1代码首先我如何创建一个可空的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下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

我希望C2被创建为Nullable,但是C1和C2都不为空。添加

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.

那么如何使用Code First获得一个可空的列?

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

推荐答案

p>没有办法说, string 属性可以被属性置空,因为如果不使用。在流畅的映射情况下,您可以将其描述为:

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)

这篇关于与ef 4.1代码首先我如何创建一个可空的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆