*
如下命令在powershell执行
The following command is executed in powershell
*
dotnet new console -n OvloadBuildFail
cd .\OvloadBuildFail\
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
*
将以下内容替换到Program.cs中
Replace the following into program.cs
*
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
namespace OvloadBuildFail
{
class Program
{
static void Main(string[] args)
{
var repo = new TestRepostory<Student>(new MyDBConext());
System.IO.File.Delete("TestDB.db");
repo.DbContext.Database.EnsureCreated();
var all = repo.DbSet.ToList();
repo.Remove(all);
var id1 = Guid.NewGuid();
repo.Add(new Student
{
Id = id1,
Name = "xiaoming",
RreportCard = new RreportCard() { Id = Guid.NewGuid(), Name = "Mathematics", Score = 60, StudentId = id1 },
});
var id2 = Guid.NewGuid();
repo.Add(new Student
{
Id = id2,
Name = "daming",
RreportCard = new RreportCard() { Id = Guid.NewGuid(), Name = "Mathematics", Score = 90 }
});
var id3 = Guid.NewGuid();
repo.Add(new Student
{
Id = id3,
Name = "me",
RreportCard = new RreportCard() { Id = Guid.NewGuid(), Name = "Mathematics", Score = 78, StudentId = id3 },
});
repo.DbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var arr = new string[] { "daming", "me" };
var list = repo.List(x => arr.Contains(x.Name), s => s.Name);
var list2 = repo.List(x => arr.Contains(x.Name), x => x.RreportCard);
Console.Read();
}
}
public class MyDBConext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Student>(x =>
{
x.HasOne(t => t.RreportCard).WithOne(t => t.Student).OnDelete(DeleteBehavior.Cascade);
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite("Data Source=TestDB.db");
}
public DbSet<Student> TestEntities { get; set; }
}
public class Student
{
public Guid Id { get; set; }
public string Name { get; set; }
public RreportCard RreportCard { get; set; }
}
public class RreportCard
{
public Guid Id { get; set; }
public string Name { get; set; }
public double Score { get; set; }
public Guid StudentId { get; set; }
public Student Student { get; set; }
}
class TestRepostory<TEntity>
where TEntity : class
{
public TestRepostory(DbContext dbContext)
{
this.DbContext = dbContext;
this.DbSet = this.DbContext.Set<TEntity>();
}
public DbContext DbContext { get; }
public DbSet<TEntity> DbSet { get; }
public virtual TEntity Add(TEntity entity)
{
this.DbSet.Add(entity);
this.DbContext.SaveChanges();
return entity;
}
public virtual TEntity Remove(TEntity entity)
{
this.DbSet.Remove(entity);
this.DbContext.SaveChanges();
return entity;
}
public virtual IList<TEntity> Remove(IList<TEntity> entities)
{
this.DbSet.RemoveRange(entities);
this.DbContext.SaveChanges();
return entities;
}
private IQueryable<TEntity> _List(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, object>> sort = null)
{
IQueryable<TEntity> queryable = this.DbSet;
if (where != null) queryable = queryable.Where(where);
if (sort != null)
{
queryable = queryable.OrderBy(sort);
}
return queryable;
}
public virtual IList<TEntity> List(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, object>> sort = null)
{
var queryable = _List(where, sort);
var list = queryable.ToList();
return list;
}
public virtual IList<TEntity> List<TProperty>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TProperty>> include, Expression<Func<TEntity, object>> sort = null)
where TProperty : class, new()
{
var queryable = _List(where, sort);
if (include != null) queryable = queryable.Include(include);
var list = queryable.ToList();
return list;
}
}
}
In vs2019, the code is compiled and runs normally. Variables list and list2 are normally obtained
在控制台执行dotnet build编译通过
Executing dotnet build successfully in the console
在vs2019中全部正常
All succeeded in vs2019
在控制台执行dotnet build编译失败
dotnet build compilation failed in consoleError output:
Program.cs(41,24): error CS0310: '“string”必须是具有公共的无参数构造函数的非抽象类型,才能用作泛型类型或方法“TestRepostory<Student>.List<TProperty>(Expression<Func<Student, bool>>, Expression<Func<Student, TProperty>>, Expression<Func<Student, object>>)”中的参数“TProperty” [\OvloadBuildFail\OvloadBuildFail\OvloadBuildFail.csproj]
生成失败。
Program.cs(41,24): error CS0310: '“string”必须是具有公共的无参数构造函数的非抽象类型,才能用作泛型类型或方法“TestRepostory<Student>.List<TProperty>(Expression<Func<Student, bool>>, Expression<Func<Student, TProperty>>, Expression<Func<Student, object>>)”中的参数“TProperty” [\OvloadBuildFail\OvloadBuildFail\OvloadBuildFail.csproj]
0 个警告
1 个错误
已用时间 00:00:01.87
dotnet --info output:
Core SDK(反映任何 global.json):
Version: 2.2.101
Commit: 236713b0b7
运行时环境:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.101\
Host (useful for support):
Version: 2.2.0
Commit: 1249f08fed
.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.502 [C:\Program Files\dotnet\sdk]
2.1.505 [C:\Program Files\dotnet\sdk]
2.1.508 [C:\Program Files\dotnet\sdk]
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.604 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.701 [C:\Program Files\dotnet\sdk]
2.1.801 [C:\Program Files\dotnet\sdk]
2.2.101 [C:\Program Files\dotnet\sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
@agocke @jaredpar any ideas?
Do we know which version of VS2019 is being used here? Need that to understand which SDK is involved for the good scenario.
So far I've been unable to reproduce this. I used the following SDK versions:
The code compiled in both cases.
I see he is using 2.2.101, which is the equivalent of 15.9 for the SDK, while VS is some 16.x build.
谢谢大家
Thanks everyone.
此问题和sdk版本有关,2.2.101会有这个问题
This issue is related to the sdk version,2.2.101 will have this problem.
在我安装当前最新版的dotnet-sdk-2.2.401后此问题得到解决
This problem is solved after I install the latest version of dotnet-sdk-2.2.401.