Question

I was wondering if it is possible (either through the Fluent Api or data annotations) to have a property map to the database as bit, hierarchyid, sql_variant, sysname, table, or timestamp when generating the database via code first. Really the most important one for me is sysname, I'm just curious on the rest. Has any done this? Thanks

Was it helpful?

Solution

Reading from here I would say no. There is no CLR type equivalent to sysname SQL type.

I also try .HasColumnType("sysname") with no succes bu an exception on a missing namespace for non primitive type name. But as there is nothing in the CLR concerning sysname, I have no namespace to provide.

Form here you can read that sysname is nvarchar(128) not null.

hope this helps

OTHER TIPS

I just sheepishly repeated the empiric approach as done 3 years ago here.

I got data types that can be put in a table by 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

(Note that sysname is in there but table isn't).

I reverse-engineered a table containing these types with entity framework power tools (beta 3, EF 6.0.0 alpha-3) and got this result:

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; }    }

You see that presently these types are not supported:

hierarchyid
sql_variant

And of course table, because it's not a column data type in the first place.

Well, this is the dumb approach supplementing tschmit007's smarter answer because, like you, I was just curious.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top