首先使用EF代码生成包含具有特殊SQL数据类型的列的表

时间:2013-03-27 14:46:41

标签: entity-framework data-annotations fluent-interface

我想知道在通过代码生成数据库时,是否有可能(通过Fluent Api或数据注释)将数据库的属性映射为bit,hierarchyid,sql_variant,sysname,table或timestamp。对我来说真正最重要的是sysname,我只是好奇其余的。有没有这样做过?感谢

2 个答案:

答案 0 :(得分:2)

here阅读我会说不。没有与sysname SQL类型等效的CLR类型。

我还尝试.HasColumnType("sysname"),但对于非基本类型名称缺少名称空间没有成功。但由于CLR中没有关于sysname的内容,我没有提供名称空间。

表单here,您可以读取sysname为nvarchar(128) not null

希望这会有所帮助

答案 1 :(得分:2)

我只是怯懦地重复了3年前所做的经验方法here

我得到的数据类型可以放在SELECT name FROM sys.systypes的表格中:

bigint
binary
bit
char
date
datetime
datetime2
datetimeoffset
decimal
float
geography
geometry
hierarchyid
image
int
money
nchar
ntext
numeric
nvarchar
real
smalldatetime
smallint
smallmoney
sql_variant
sysname
text
time
timestamp
tinyint
uniqueidentifier
varbinary
varchar
xml

(请注意sysname在那里,但table不在。

我使用实体框架电动工具(beta 3,EF 6.0.0 alpha-3)反向设计了包含这些类型的表,并得到了这个结果:

public partial class AllType{
    public int Id { get; set; }
    public Nullable<long> bigint { get; set; }
    public byte[] binary { get; set; }
    public Nullable<bool> bit { get; set; }
    public string @char { get; set; }
    public Nullable<System.DateTime> date { get; set; }
    public Nullable<System.DateTime> datetime { get; set; }
    public Nullable<System.DateTime> datetime2 { get; set; }
    public Nullable<System.DateTimeOffset> datetimeoffset { get; set; }
    public Nullable<decimal> @decimal { get; set; }
    public Nullable<double> @float { get; set; }
    public System.Data.Entity.Spatial.DbGeography geography { get; set; }
    public System.Data.Entity.Spatial.DbGeometry geometry { get; set; }
    public byte[] image { get; set; }
    public Nullable<int> @int { get; set; }
    public Nullable<decimal> money { get; set; }
    public string nchar { get; set; }
    public string ntext { get; set; }
    public Nullable<decimal> numeric { get; set; }
    public string nvarchar { get; set; }
    public Nullable<float> real { get; set; }
    public Nullable<System.DateTime> smalldatetime { get; set; }
    public Nullable<short> smallint { get; set; }
    public Nullable<decimal> smallmoney { get; set; }
    public string sysname { get; set; }
    public string text { get; set; }
    public Nullable<System.TimeSpan> time { get; set; }
    public byte[] timestamp { get; set; }
    public Nullable<byte> tinyint { get; set; }
    public Nullable<System.Guid> uniqueidentifier { get; set; }
    public byte[] varbinary { get; set; }
    public string varchar { get; set; }
    public string xml { get; set; }    }

您看到目前不支持这些类型:

hierarchyid
sql_variant

当然还有table,因为它首先不是列数据类型。

嗯,这是补充tschmit007更聪明的答案的愚蠢方法,因为和你一样,我只是好奇。