Setting string to be sql type of “varchar” instead of “nvarchar”
-
23-09-2019 - |
문제
I have the following mapping:
public class LogEntryMap
{
public LogEntryMap()
{
Map.Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Context).CustomSqlType("varchar").Length(512);
}
}
However, using SchemaExport
to generate the database in SQL Server 2008, the script generated ignores the length so in effect it ends up being a varchar
with length of 1:
create table OV_SAC.dbo.[LogEntry] (
Id BIGINT IDENTITY NOT NULL,
Context varchar null,
primary key (Id)
)
.CustomSqlType("varchar 512")
throws an exception. And without defining the CustomSqlType
, strings are mapped to nvarchar
(which does respect the Length
property).
Any suggestions?
해결책
Use .CustomType("AnsiString")
instead of default "String"
and NHibernate will use varchar
instead of nvarchar
.
다른 팁
If you wanted all of your strings to be mapped to varchar instead of nvarchar you could consider using a convention:
/// <summary>
/// Ensures that all of our strings are stored as varchar instead of nvarchar.
/// </summary>
public class OurStringPropertyConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (instance.Property.PropertyType == typeof (string))
instance.CustomType("AnsiString");
}
}
You mappings could then go back to a simple mapping:
Map(x => x.Context);
Just make sure you remember to tell Fluent NH to use the convention:
var configuration = new Configuration();
configuration.Configure();
Fluently
.Configure(configuration)
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf<Widget>()
.Conventions.Add<OurStringPropertyConvention>()
)
.BuildSessionFactory();
Doh.
Map(x => x.Context).CustomSqlType("varchar (512)");
create table OV_SAC.dbo.[LogEntry] (
Id BIGINT IDENTITY NOT NULL,
Context varchar (512) null,
primary key (Id)
)
이를 사용하려고 노력해야합니다.그것은 당신을 도울 수 있습니다 :
var data = {
'__metadata': { 'type': 'SP.Data.TimesheetListItem' },
'Timesheet_x0020_Page':
{
'__metadata': { 'type': 'SP.FieldUrlValue' },
'Description': 'Google',
'Url': 'http://google.com'
},
'State': state
};
.