问题描述
您好,
我似乎在数据库中存在影响代码优先约束定义的空格问题。
I seem to have an issue with white spaces in the DB that effect code first constraint definitions.
我有以下表格(已经存在于数据库中):
I have the following tables (which already exists in the db):
CREATE TABLE [dbo]。[WebTrackingIds](
[ TrackingId] [varchar](50)NOT NULL,
[DateIssued] [datetime] NOT NULL)
CREATE TABLE [dbo].[WebTrackingIds](
[TrackingId] [varchar](50) NOT NULL,
[DateIssued] [datetime] NOT NULL)
TrackingId是主键
TrackingId is primary key
CREATE TABLE [dbo]。[WebTrackingItems](
[TrackingId] [varchar](50)NOT NULL,
[ItemName] [varchar](40)NOT NULL,
[ItemValue] [varchar](255)NOT NULL,
[DateTracked] [datetime] NOT NULL)
CREATE TABLE [dbo].[WebTrackingItems](
[TrackingId] [varchar](50) NOT NULL,
[ItemName] [varchar](40) NOT NULL,
[ItemValue] [varchar](255) NOT NULL,
[DateTracked] [datetime] NOT NULL)
TrackingId + ItemName是主键
TrackingId+ItemName are primary keys
I创建了以下模型和数据配置以匹配上表:
I created the following models and data configuration to match the above tables:
模型:
public class TrackingItem
{
public string TrackingId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
public DateTime DateTracked { get; set; }
}
public class UserTracking
{
public string TrackingId { get; set; }
public DateTime DateIssued { get; set; }
public virtual ICollection<TrackingItem> TrackingItems { get; set; }
}
数据conf:
public class UserTrackingConfig : EntityConfiguration<UserTracking>
{
public UserTrackingConfig()
{
this.HasKey(t => t.TrackingId);
this.HasMany(t => t.TrackingItems).WithRequired().HasConstraint((ut, ti) => ut.TrackingId == ti.TrackingId);
this.MapSingleType(t => new
{
TrackingId = t.TrackingId,
DateIssued = t.DateIssued,
}).ToTable("WebTrackingIds");
}
}
public class TrackingItemConfig : EntityConfiguration<TrackingItem>
{
public TrackingItemConfig()
{
this.HasKey(t => new { t.TrackingId, t.Name });
this.MapSingleType(t => new
{
ItemName = t.Name,
ItemValue = t.Value,
DateTracked = t.DateTracked,
TrackingId = t.TrackingId,
}).ToTable("WebTrackingItems");
}
}
然而目前在数据库中的数据中包含空白空间,例如用于trackingId 'E7539AA7-E5FA-41FE-9F60-214DE9C0D962'
However the data currently in the database contains white spaces, for example for trackingId 'E7539AA7-E5FA-41FE-9F60-214DE9C0D962'
在值WebTrackingIds.TrackingId列是 'E7539AA7-E5FA-41FE-9F60-214DE9C0D962'
the value in WebTrackingIds.TrackingId column is 'E7539AA7-E5FA-41FE-9F60-214DE9C0D962'
但在WebTrackingItems.TRackingId列中的相应的值是"E7539AA7-E5FA-41FE-9F60-214DE9C0D962&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; '
but the corresponding value in WebTrackingItems.TRackingId column is 'E7539AA7-E5FA-41FE-9F60-214DE9C0D962 '
因此永远不会填充UserTracking的TrackingItems集合
Therefore the collection of TrackingItems for UserTracking is never being populated
在定义数据配置时是否有办法强制忽略空格?到目前为止,我的所有尝试都导致错误,因为不允许操作。
Is there a way to force ignore white spaces when defining data configuration? So far all my attempts caused an error as operation was not allowed.
谢谢
Naomi
  ;
推荐答案
在EF中没有办法原生地执行此操作,您最好的选择是创建一个视图来剥离空白区域,然后映射到该视图,就好像它是一个表格一样。这当然会产生性能影响,因此理想的解决方案是更新
数据库值以删除空格。
There isn't a way to do this natively in EF, your best bet would be to create a view that strips out the white space and then map to that view as if it were a table. This will of course have performance implications so the ideal solution would be to update the database values to remove the white space.
~Rowan
这篇关于使用代码优先映射时出现白色问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!