C# EF Core没有';t加载通过SqlRaw添加到DB的实体

C# EF Core没有';t加载通过SqlRaw添加到DB的实体,c#,entity-framework,sqlite,entity-framework-core,in-memory-database,C#,Entity Framework,Sqlite,Entity Framework Core,In Memory Database,我使用了EF-Core。当我试图找到通过SqlRaw命令添加到DB中的实体时,First()方法返回null,而我认为它不应该返回null。我想知道为什么 但是: 如果我使用int作为model类的键,则没有问题 如果我通过DbContext添加实体,一切都会如我所期望的那样工作 测试等级: [Fact] public void Test1() { var services = new ServiceCollection(); va

我使用了
EF-Core
。当我试图找到通过SqlRaw命令添加到DB中的实体时,
First()
方法返回null,而我认为它不应该返回null。我想知道为什么

但是:

  • 如果我使用
    int
    作为model类的键,则没有问题

  • 如果我通过
    DbContext
    添加实体,一切都会如我所期望的那样工作

测试等级:

    [Fact]
    public void Test1()
    {
        var services = new ServiceCollection();
        var connectionStringBuilder = new SqliteConnectionStringBuilder()
        {
            DataSource = ":memory:",
        };
        var connection = new SqliteConnection(connectionStringBuilder.ToString());
        services.AddDbContext<MyDbContext>(options =>
            options
            .UseSqlite(connection));
        var serviceProvider = services.BuildServiceProvider();
        var context = serviceProvider.GetService<MyDbContext>();
        context.Database.OpenConnection();
        context.Database.EnsureCreated();


        var id = Guid.NewGuid();
        AddDevice(context, id);


        Assert.NotEmpty(context.Devices); // OK
        Assert.NotNull(context.Devices.First(x => x.Id == new DeviceId(id)); // Fail
    }

    private void AddDevice(MyDbContext context, Guid id)
    {
        string sqlInsert = $"INSERT INTO \"Devices\" (\"Id\") VALUES ('{id}')";
        context.Database.ExecuteSqlRaw(sqlInsert);
    }
[事实]
公共void Test1()
{
var services=newservicecolection();
var connectionStringBuilder=new-SqliteConnectionStringBuilder()
{
DataSource=“:内存:”,
};
var connection=newsqliteconnection(connectionStringBuilder.ToString());
services.AddDbContext(选项=>
选择权
.UseSqlite(连接));
var serviceProvider=services.BuildServiceProvider();
var context=serviceProvider.GetService();
context.Database.OpenConnection();
context.Database.recreated();
var id=Guid.NewGuid();
AddDevice(上下文,id);
Assert.NotEmpty(context.Devices);//确定
Assert.NotNull(context.Devices.First(x=>x.Id==newdeviceid(Id));//失败
}
私有void AddDevice(MyDbContext上下文,Guid id)
{
字符串sqlInsert=$“插入到\“设备\”(\“Id\”)值(“{Id}”)”;
context.Database.ExecuteSqlRaw(sqlInsert);
}
模型类:

public class Device
{
    public DeviceId Id { get; private set; }
    private string _token; 
    
    private Device()
    {
    }

    public Device(string token)
        : this()
    {
        Id = new DeviceId(Guid.NewGuid());
        _token = token;
    }
}

public class DeviceId : IEquatable<DeviceId>
{
    public Guid Value { get; }

    public DeviceId(Guid guid)
    {
        this.Value = guid;
    }

    public bool Equals(DeviceId other)
    {
        return this.Value.Equals(other.Value);
    }
}
公共类设备
{
公共设备Id{get;private set;}
私有字符串\u令牌;
专用设备()
{
}
公共设备(字符串令牌)
:此()
{
Id=新设备Id(Guid.NewGuid());
_令牌=令牌;
}
}
公共类设备ID:IEquatable
{
公共Guid值{get;}
公用设备ID(Guid)
{
this.Value=guid;
}
公共布尔等于(设备ID其他)
{
返回此.Value.Equals(其他.Value);
}
}
配置类:

internal class DeviceConfiguration : IEntityTypeConfiguration<Device>
{
    public void Configure(EntityTypeBuilder<Device> builder)
    {
        builder.ToTable("Devices");

        builder.HasKey(x => x.Id);
        builder.Property(x => x.Id)
            .HasConversion(
                v => v.Value,
                v => new DeviceId(v));

        builder.Property<string>("_token").HasColumnName("Token");
    }
}
内部类设备配置:IEntityTypeConfiguration
{
公共void配置(EntityTypeBuilder)
{
建造商:可折叠(“设备”);
HasKey(x=>x.Id);
builder.Property(x=>x.Id)
.哈斯转换(
v=>v.值,
v=>新设备ID(v));
建筑商财产(“_标记”)。HasColumnName(“标记”);
}
}

为什么要将GUID ID与类过度复杂化?为什么不创建一个实体并将其添加到DbContext而不是原始SQL?这可能只是一个示例场景,但从最简单的事情开始。是什么让EF以其设计的方式管理事物而产生额外的复杂性/偏差?为什么要将实体过度复杂化GUID ID带有一个类?为什么不创建一个实体并将其添加到DbContext而不是原始SQL?这可能只是一个示例场景,但从最简单的事情开始。是什么让EF以其设计的方式管理事物带来了额外的复杂性/偏差?