Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.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# 实体框架-如何在一侧映射所需属性,而另一侧没有导航属性_C#_.net_Entity Framework - Fatal编程技术网

C# 实体框架-如何在一侧映射所需属性,而另一侧没有导航属性

C# 实体框架-如何在一侧映射所需属性,而另一侧没有导航属性,c#,.net,entity-framework,C#,.net,Entity Framework,我有以下两个POCO(产品和用户)和一个复杂类型(跟踪) tracking类是跟踪信息的包装,包含其他跟踪属性(创建日期、更新日期等),并满足其他用途的接口,但现在我关心的是映射产品TrackingInfo和用户之间的关系 每个产品必须有一个关联的用户,该用户映射到TrackingInfo.CreatedBy属性 问题是我不想在用户返回产品时创建导航属性,也就是说,我不想创建一个ICollection产品属性 我不确定首先应该如何在EntityFramework代码中完成关系或复杂类型映射。我有

我有以下两个POCO(
产品
用户
)和一个复杂类型(
跟踪

tracking类是跟踪信息的包装,包含其他跟踪属性(创建日期、更新日期等),并满足其他用途的接口,但现在我关心的是映射产品
TrackingInfo
用户之间的关系

每个
产品
必须有一个关联的
用户
,该用户映射到
TrackingInfo.CreatedBy
属性

问题是我不想在用户返回产品时创建导航属性,也就是说,我不想创建一个
ICollection产品
属性

我不确定首先应该如何在EntityFramework代码中完成关系或复杂类型映射。我有一个映射类,如下所示:

public class ProductMap : EntityTypeConfiguration<Product>
{
    public ProductMap()
    {
         this.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName("Id");

         // Complex type mapping here?
    }

}
公共类ProductMap:EntityTypeConfiguration
{
公共产品地图()
{
this.Property(t=>t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity).HasColumnName(“Id”);
//这里是复杂类型映射吗?
}
}
我怎么能

  • 映射TrackinInfo复杂类型
  • 映射CreatedBy->用户单向关系

  • 如果不需要导航属性,请不要定义它们:

    public ProductMap()
    {
        this.HasRequired(p => p.Tracking)
            .WithMany()
            .HasForeignKey(p => p.TrackingId);
    }
    

    通过这种方式,您可以通过
    跟踪
    访问产品用户,但无法从用户处获取产品,因为在
    跟踪
    中定义的
    产品
    没有导航属性。我建议创建一个包含
    跟踪
    属性及其配置的抽象基类:

    class Product() {
    
        Guid Id { get; protected set; }
        Tracking TrackingInfo { get; protected set; }
    
        // Some other properties
    }
    
    
    class User() {
    
        Guid Id { get; protected set; }
    
        // Some other properties
    }
    
    
    class Tracking() {
    
          User CreatedBy { get; protected set; }
          Guid CreatedById { get; protected set; }
    
          // Some other properties
    }
    
    public abstract class Tracking
    {
        public Guid CreatedById { get; set; }
        public virtual User CreatedBy { get; set; }
    }
    
    public abstract class TrackingConfig<T> : EntityTypeConfiguration<T> where T: Tracking
    {
        public TrackingConfig()
        {
            HasRequired( t => t.CreatedBy )
                .WithMany()
                .HasForeignKey( t => t.CreatedById );
        }
    }
    
    public class Product : Tracking
    {
        public Guid Id { get; set; }
    }
    
    public class ProductConfig : TrackingConfig<Product>
    {
        public ProductConfig()
        {
        }
    }
    

    这似乎是一个明智的解决方案。我的POCO已经从基类继承,因此重构将是简单的。这似乎是EF的一个小缺点,即复杂属性不能包含导航属性。
        protected override void OnModelCreating( DbModelBuilder modelBuilder )
        {
            base.OnModelCreating( modelBuilder );
    
            modelBuilder.Configurations.Add( new ProductConfig() );