如何在复杂类型上指定关系? [英] How to specify a relation on a complex type?

查看:70
本文介绍了如何在复杂类型上指定关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用EF代码在复杂类型上定义两个关系,但InvalidOperation异常 {的结果不成功。表达式'p => p.AuditInfo.CreatedBy'不是有效的属性表达式。表达式应表示一个属性:C#:'t => t.MyProperty'VB.Net:'Function(t)t.MyProperty'。}

I'm using EF code first to define two relations on a complex type with an unsuccessful result of an InvalidOperation Exception "{"The expression 'p => p.AuditInfo.CreatedBy' is not a valid property expression. The expression should represent a property: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'."}"

什么我做错了吗?

相关代码:

public class AuditInfo
{
    public DateTime? CreatedOn { get; internal set; }
    public Guid? CreatedById { get; internal set; }
    public User CreatedBy { get; internal set; }

    public DateTime? ModifiedOn { get; internal set; }
    public Guid? ModifiedById { get; internal set; }
    public User ModifiedBy { get; internal set; }

    public byte[] ModifiedTimestamp;
}

public class User
{
    public Guid Id { get; internal set; }
    public string Email { get; internal set; }

    public AuditInfo AuditInfo { get; internal set; }
}

class UserMap : EntityTypeConfiguration<User>
{
    public UserMap()
    {
        this.HasOptional(p => p.AuditInfo.CreatedBy).WithMany().HasForeignKey(p => p.AuditInfo.CreatedById);
        this.HasOptional(p => p.AuditInfo.ModifiedBy).WithMany().HasForeignKey(p => p.AuditInfo.ModifiedById);
    }
}

public class Test3DbContext : DbContext
{
    public DbSet<User> Users { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<AuditInfo>();
        modelBuilder.Configurations.Add(new UserMap());
    }
}

数据库表:

User {ID,Email,...,CreatedById,ModifiedById,CreatedOn,ModifiedOn,...}

User { Id, Email, ..., CreatedById, ModifiedById, CreatedOn, ModifiedOn, ... }

推荐答案

将复杂类型视为值对象,并将其附加到要创建的表的末尾。因此,用户将具有ID列,然后是电子邮件列,然后是AuditInfo列。唯一的区别在于代码内,而不是数据库内。因此,复杂类型由其父类跟踪,并且没有外键。如果您正在寻找其他东西,您将需要重新考虑您的设计。

A complex type is treated like a value object and is appended to the end of the table that is being created. So, User will have columns Id, then email, then the AuditInfo columns. The only separation is within the code, not in the database. Therefore, complex types are tracked by their parent class, and have no foreign keys. You will need to rethink your design if you are looking for something else.

更新:

如果您要我们正在寻找的只是确保将CreatedBy / ModifiedBy映射为外键约束,然后使用ComplexType看来这是不可能的。您将必须在用户表中放入完整的审核信息。在复杂类型文档

If what you are looking for is just to make sure that CreatedBy/ModifiedBy is mapped for foreign key constraints, then it appears this is not possible using a ComplexType. You would have to put the full audit info in the user table. This is specified in the Complex Type documentation


复杂类型不能包含导航属性。

Complex type cannot contain navigation properties.

这篇关于如何在复杂类型上指定关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆