EF核心,nvarchar的null映射不正确

时间:2016-08-16 21:07:12

标签: entity-framework-core

在我的模型构建器中,我有这个映射:

entity.Property(e => e.topic)
      .HasColumnType("nvarchar(50)")
      .HasColumnName("topic");

对于此字段:

public string topic { get; set; }

但是当我查询包含列"主题"的表时(对于每个模型中nvarchar类型的所有列都相同),DB值null用字符串" null"映射。没有值null。

这是EF核心的错误吗?或者我错误地映射了该列?

更新

以下是实际代码:

  modelBuilder.Entity<Bait_dom>(entity =>
  {
    entity.HasKey(e => e.blid)
        .HasName("PK_seo_lb_dom");

    entity.ToTable("bait_dom");

    entity.Property(e => e.blid)
        .HasColumnName("blid");

    entity.Property(e => e.customerid)
      .IsRequired()
      .HasColumnName("customerid")
      .HasMaxLength(50);

    entity.Property(e => e.url)
            .IsRequired()
            .HasColumnName("url")
            .HasMaxLength(100);

    entity.Property(e => e.mozda)
      .HasColumnName("mozda");

    entity.Property(e => e.kwpos)
      .HasColumnName("kwpos");

    entity.Property(e => e.moztrust)
      .HasColumnName("moztrust");

    entity.Property(e => e.ahrefdr)
      .HasColumnName("ahrefdr");

    entity.Property(e => e.topic)
      .HasColumnType("nvarchar(50)")
      .HasColumnName("topic");

    entity.Property(e => e.status)
      .HasColumnName("status")
      .HasMaxLength(10);

    entity.Property(e => e.createdon)
        .IsRequired()
        .HasColumnName("createdon");

    entity.Property(e => e.updatedon)
        .HasColumnName("updatedon");
  });

public virtual DbSet<Bait_dom> Bait_dom { get; set; }

public class Bait_dom
{
    public int blid { get; set; }
    public string customerid { get; set; }
    public string url { get; set; }
    public decimal? mozda { get; set; }
    public int? kwpos { get; set; }
    public decimal? moztrust { get; set; }
    public decimal? ahrefdr { get; set; }
    public string topic { get; set; }
    public string status { get; set; }
    public DateTime? askedon { get; set; }
    public DateTime createdon { get; set; }
    public DateTime? updatedon { get; set; }
}

这里的表定义:

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[bait_dom](
    [blid] [int] IDENTITY(1,1) NOT NULL,
    [customerid] [nvarchar](50) NOT NULL,
    [url] [nvarchar](100) NOT NULL,
    [mozda] [numeric](5, 2) NULL,
    [kwpos] [int] NULL,
    [moztrust] [numeric](4, 2) NULL,
    [ahrefdr] [numeric](4, 2) NULL,
    [topic] [nvarchar](50) NULL,
    [status] [nvarchar](10) NULL,
    [askedon] [date] NULL,
    [createdon] [datetime] NOT NULL,
    [updatedon] [datetime] NULL,
CONSTRAINT [PK_seo_lb_dom] PRIMARY KEY CLUSTERED 
(
[blid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

最后是一个查询:

  List<SiteData> ret = (from d in _db.Bait_dom
           join li in _db.Bait_links on new { d.url, d.customerid } equals new { li.url, li.customerid } into lin
           from l in lin.DefaultIfEmpty()
           where d.customerid == _user.guid
           group new { d, l } by d into dl
           select new SiteData
           {
             dom = dl.Key,
             links = dl.Where(l => l.l != null).Select(l => l.l).ToList(),
             hm = dl.Where(l => l.l != null).Count(l => l.l.status != "planned"),
             hmp = dl.Where(l => l.l != null).Count(l => l.l.status == "planned"),

             asked = dl.Key.askedon,
             lastLink = dl.Where(l => l.l != null && l.l.status != "planned").Max(l => l.l.publishedon),
             nextLink = dl.Where(l => l.l != null && l.l.status == "planned").Min(l => l.l.publishedon)

           })
          .ToList();

0 个答案:

没有答案