EF Core 3.1 property is of type 'Nullable<Guid>' which is not supported by current database provider

0

Moving over from .net Framework EF 6.4, did something similar for years. Just trying to use a Guid datatype with SQL Server db, getting this error - this should just work, right?? If I remove applyconfigurations in the DbContext.OnModelCreating then it works (must be using default). System.InvalidOperationException HResult=0x80131509 Message=The property 'User.CreatedById' is of type 'Nullable' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'. Source=Microsoft.EntityFrameworkCore

Note: Don't want to use EF attributes on classes to keep entity classes clean and not requiring dependency to EF core packages.

public interface IEntity : INotifyPropertyChanged
{
    Guid? CreatedById { get; set; }
    DateTime CreatedOn { get; set; }
    Guid Id { get; set; }
}

public class EntityBase : NotifyPropertyChangedBase, IEntity
{
    public Guid? CreatedById { get; set; }
    public DateTime CreatedOn { get; set; }
    public Guid Id { get; set; }
}

public class User : EntityBase
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
}

public abstract class ClassMapBase<TEntity> : IEntityTypeConfiguration<TEntity> 
    where TEntity : class, IEntity, new()
{
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
    builder.HasKey(x => x.Id);
    builder.Property(x => x.Id).HasColumnType("uniqueidentifier").IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("newid()");
    builder.Property(x => x.CreatedOn).HasColumnType("datetime").IsRequired();
    builder.Property(x => x.CreatedById).HasColumnType("int").IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("newid()");
}

}

public class UserClassMap : ClassMapBase<User>
{

    public override void Configure(EntityTypeBuilder<User> builder)
    {
        base.Configure(builder);
        builder.Property(x => x.Email);
        builder.Property(x => x.LastName).IsRequired();
        builder.Property(x => x.FirstName).IsRequired();
    }
}
 public class LaaDbContext : DbContext, ILaaDbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
        base.OnModelCreating(modelBuilder);
    }

    public DbSet<User> Users { get; set; }

    public IList<User> GetUsers()
    {
        return Set<User>().AsNoTracking().ToList();
    }
}

SQL Server Script to create table:

CREATE TABLE [dbo].[Users](
    [Id] [uniqueidentifier] NOT NULL CONSTRAINT [DF_Users_Id]  DEFAULT (newid()),
    [Email] [nvarchar](256) NOT NULL,
    [FirstName] [nvarchar](128) NOT NULL,
    [LastName] [nvarchar](128) NOT NULL,
    [CreatedOn] [datetime] NOT NULL CONSTRAINT [DF_Users_CreatedOn]  DEFAULT (getdate()),
    [CreatedById] [uniqueidentifier] NULL,
    CONSTRAINT [PK_dbo.Users] PRIMARY KEY CLUSTERED ([Id] 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]
entity-framework-core
.net-standard-2.0
asked on Stack Overflow Jan 3, 2020 by dan

1 Answer

0

oops, copy/paste error - the ClassMapBase had columntype int, when I changed it to uniqueidentifier, it worked.

builder.Property(x => x.CreatedById).HasColumnType("uniqueidentifier").IsRequired().ValueGeneratedOnAdd().HasDefaultValueSql("newid()");
answered on Stack Overflow Jan 3, 2020 by dan

User contributions licensed under CC BY-SA 3.0