Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用EF Core从数据库选择复杂类型时出现问题_C#_Entity Framework Core - Fatal编程技术网

C# 使用EF Core从数据库选择复杂类型时出现问题

C# 使用EF Core从数据库选择复杂类型时出现问题,c#,entity-framework-core,C#,Entity Framework Core,我尝试使用复杂类型if-EF-Core。 我的桌子结构 我的班级结构是 public class UserId { public Guid Value { getl } private UserId() { } public UserId (Guid newId) { //check and assign } } public class User { public UserId Id { get; } public N

我尝试使用复杂类型if-EF-Core。 我的桌子结构

我的班级结构是

public class UserId 
{
    public Guid Value { getl }
    private UserId() { }
    public UserId (Guid newId) { 
        //check and assign
    }
}

public class User
{
     public UserId Id { get; }
     public Name Name { get; }
     private User() { }
     public User(Name name) {  
         //.... anti corruption 
     }
}

public class Name 
{
     public string First { get; } 
     public string Last { get; }

     private Name() { }
     public Name(string firstName, string lastName)
     {
          //anti curruption
     }

}
这里是关于模型创建的

modelBuilder.Entity<User>()
    .ToTable("User");
modelBuilder.Entity<User>()
    .HasKey(u => u.Id);
modelBuilder.Entity<User>()
    .OwnsOne(u => u.Name, un =>
    {
        un.Property(x => x.First).HasColumnName("FirstName");
        un.Property(x => x.Last).HasColumnName("LastName");
    });
我的打印结果方法

static void PrintResult(string label, User u)
{
    Console.WriteLine($"{label} >>>>>");
    if (u.Name == null)
    {
        Console.WriteLine("Could not read Name from database");
    }
    else
    {
        Console.WriteLine("Read Name from database success");
    }
}
有人能告诉我我做错了什么吗?

为什么我必须使用.AsNoTracking?

事实上,此问题不会在此处发布的模型中重现。但它与链接中的一个相同,区别在于模型中PK属性的类型-使用已知的基元类型时没有问题,但您使用的是自定义id类-还有另一个DDD“sugar”,但具有不正确/缺失的相等实现

如果不为id类实现值语义,EF Core将通过引用对其进行比较,从而找不到所属实体类型所需的“所有者PK”。无跟踪查询没有这样的搜索,这就是为什么它“工作”

正确的操作是在
UserId
类中实现值相等语义(用作
User.Id
属性的一种类型)

例如加入

public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => obj is UserId other && Value == other.Value;

私有默认构造函数和反腐败代码的用途是什么?欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后,你的问题包括完整的源代码,你有一个,它可以被编译和测试的其他人。您提供的代码无法重现问题。@我猜在为EF创建私有构造函数时,Progman是“类似DDD”的实体,而为其他人创建public(带参数)。@TosZa BaaTeeSood您能提供什么版本的EF.core?在这一系列代码中,隐式跟踪查询的第一个实例实际上不是来自数据库,而是您的原始实体,可以使用
Debug.Assert(ReferenceEquals(u1,newUser))之类的东西进行验证为什么属性为
null
是个谜-您确定没有隐藏的代码执行此操作吗。这些DDD概念和它们在EFC中的实现看起来总是可疑的。这是工作!!!谢谢你帮我理解。下次我会改进我的问题。
static void PrintResult(string label, User u)
{
    Console.WriteLine($"{label} >>>>>");
    if (u.Name == null)
    {
        Console.WriteLine("Could not read Name from database");
    }
    else
    {
        Console.WriteLine("Read Name from database success");
    }
}
public class UserId
{
    public Guid Value { get; }
    protected UserId() { }

    public UserId(Guid id)
    {
        if (id == Guid.Empty)
            throw new ArgumentException("UserId must not be Empty");

        Value = id;

    }
}
public override int GetHashCode() => Value.GetHashCode();
public override bool Equals(object obj) => obj is UserId other && Value == other.Value;