【问题标题】:Fluent mapping and relation table with Active flag带有 Active 标志的 Fluent 映射和关系表
【发布时间】:2015-06-01 09:42:23
【问题描述】:

我有一个关系表,但有一个额外的列Active bit not null

我不明白我应该如何在 EF6 中映射,我有 3 个表,FooBarFooBar,其中 FooBar 是关系表

FooBar {
   FooId: int (Key and FK)
   BarId: int (Key and FK)
   Active: bit
}

Foo 实体

public class Foo
{
   public int Id { get;set; }  
   public ICollection<FooBar> Bars { get; set; } 
   ...
}

FooBar 实体

public class FooBar
{
   public Foo Foo { get;set; }  
   public Bar Bar { get;set; }  
   public bool Active { get;set; }
}

现在到问题,EF 配置 FooBar 配置

public class FooBarConfiguration : EntityTypeConfiguration<FooBar>
    {
        public FooBarConfiguration()
        {
            HasKey(fb => new[] {fb.Foo.Id, pv.Bar.Id}); //Is this correct?

            Property(pv => pv.Active);

            ToTable("ProductVehicle");
        }
    }

我不知道 Foo 配置应该是这样的,所以我卡住了,尝试了类似的东西

public class FooConfiguration : EntityTypeConfiguration<Foo>
{
    public FooConfiguration()
    {
        HasKey(f => f.Id);          

        HasMany(f => f.Bars)
            .WithRequired(f => p.Foo)
            .HasForeignKey(f => f.Foo.Id);

        ToTable("Foo");
    }
}

我明白了

“System.InvalidOperationException”类型的第一次机会异常 发生在EntityFramework.dll中

附加信息:属性表达式 'f => f.Foo.Id' 无效。表达式应该代表一个属性:C#: 't => t.MyProperty' VB.Net:'函数(t)t.MyProperty'。指定时 多个属性使用匿名类型:C#: 't => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'。

我也改变了 FooConfig

HasMany(f => p.Bars)
    .WithRequired(fb => fb.Foo)
    .Map(map => map.MapKey("FooId"));

我得到了一点时间,现在它在 FooBar 配置上失败了

“System.InvalidOperationException”类型的第一次机会异常 发生在EntityFramework.dll中

附加信息:属性表达式 'fb => new [] {fb.Foo.Id, fb.Bar.Id}' 无效。表达式应该 表示一个属性: C#: 't => t.MyProperty' VB.Net: 'Function(t) t.MyProperty'。指定多个属性时使用匿名 类型:C#:'t => new { t.MyProperty1, t.MyProperty2 }' VB.Net: 'Function(t) New With { t.MyProperty1, t.MyProperty2 }'.

编辑:我最终得到了这个解决方案,让它更受域驱动,

public class ProductVehicle
{
    private Product _product;
    private Vehicle _vehicle;
    internal int ProductId { get; set; }
    internal int VehicleId { get; set; }

    public Product Product
    {
        get { return _product; }
        set
        {
            _product = value;
            ProductId = value.Id;
        }
    }

    public Vehicle Vehicle
    {
        get { return _vehicle; }
        set
        {
            _vehicle = value;
            VehicleId = value.Id;
        }
    }

    public bool Active { get; set; }
}

【问题讨论】:

    标签: c# entity-framework entity-framework-6


    【解决方案1】:

    你需要给 FooBar 添加一个属性,比如 FooId:

    public class FooBar
    { 
    ...
       public int FooId { get { return Foo.Id;}}
    }
    

    然后将代码改为:

     HasMany(f => f.Bars)
            .WithRequired(f => p.Foo)
            .HasForeignKey(f => f.FooId);
    

    【讨论】:

      【解决方案2】:

      您正在尝试将导航属性的属性用作键或外键,这是不允许的。连接表本身(和相应的实体)应该有外键和复合主键的列(和属性)

      将外键添加到您的连接表模型中,该模型也用作复合主键

      public class FooBar
      {
         public int FooId { get; set; }
         public int BarId { get; set; }
      
         public Foo Foo { get;set; }  
         public Bar Bar { get;set; }  
         public bool Active { get;set; }
      }
      

      将这些外键配置为主键

      public class FooBarConfiguration : EntityTypeConfiguration<FooBar>
      {
          public FooBarConfiguration()
          {
              HasKey(fb => new[] { fb.FooId, fb.BarId });
      
              // A reversed version could be placed in the Foo configuration
              // But only one is necessary
              HasRequired(fb => fb.Foo)
                  .WithMany(fb => fb.Bars)
                  .HasForeignKey(fb => fb.FooId);
      
              // Something similar for relationship with Bar
      
              Property(pv => pv.Active);
      
              ToTable("ProductVehicle"); // You mean FooBar right? ;)
          }
      }
      

      【讨论】:

      • 操作,产品名称漏掉了:D 是的,这就是我目前所拥有的。但是从 UI 更新时会有点难看。我没有 100% 完成这个用户故事,如果我没有问题,我会标记为答案
      • 我最终得到了一个稍微不同的解决方案,使其更受域驱动,请参阅编辑
      猜你喜欢
      • 1970-01-01
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多