2

我想知道在首先通过代码生成数据库时,是否有可能(通过 Fluent Api 或数据注释)将数据库的属性映射为位、hierarchyid、sql_variant、sysname、表或时间戳。对我来说真正最重要的是系统名,我只是对其余的感到好奇。有没有人这样做过?谢谢

4

2 回答 2

2

从这里读我会说不。没有与 sysname SQL 类型等效的 CLR 类型。

我也尝试.HasColumnType("sysname")在非原始类型名称的缺失命名空间上出现异常,但没有成功。但由于 CLR 中没有关于 sysname 的内容,因此我没有名称空间可提供。

在这里你可以读到 sysname是nvarchar(128) not null.

希望这可以帮助

于 2013-03-27T15:13:59.143 回答
2

我只是不好意思地重复了 3 年前在这里所做的经验方法。

我得到了可以通过以下方式放入表中的数据类型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 更聪明的答案的愚蠢方法,因为和你一样,我只是好奇。

于 2013-03-27T16:23:32.973 回答