实体框架代码隐含复合密钥的首选可选一对多关系

时间:2013-07-24 20:38:23

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

更新:在GitHub上创建了sample project

在我现有的数据库中,我有一个没有密钥的通用审计表,但我想首先使用EF代码插入此审计表。

现有报告根据AffectedId和EntityId进行审核,其中EntityId在一堆地方进行了硬编码。
我反过来设计了数据库,但这些表之间没有明确的关系...所以这里是基础POCO对象(我也不能建立关系,它是现有系统)

public class Audit
{
    public int Id { get; set; }
    public int EntityId { get; set; }
    public string AffectedId { get; set; }       
    public string NewValue { get; set; }
}

public class Action1
{
    public int Id { get; set; }
    public string Desc { get; set; }
}   

public class Action2
{
    public int Id { get; set; }
    public string Desc { get; set; }
}

我想我希望POCO对象看起来像这样

public class Audit
{
    public int Id { get; set; }
    public int EntityId { get; set; }
    public string AffectedId { get; set; }       
    public string NewValue { get; set; }
    public virtual Action1 Action1 { get; set; } // but I don't want this to change audit table
    public virtual Action2 Action2 { get; set; } // but I don't want this to change audit table
}

public class Action1
{
    public Action1() {this.Audits = new List<Audit>();}
    public int Id { get; set; }
    public string Desc { get; set; }
    public virtual ICollection<Audit> Audits { get; set; }
}   

public class Action2
{
    public Action2() {this.Audits = new List<Audit>();}
    public int Id { get; set; }
    public string Desc { get; set; }
    public virtual ICollection<Audit> Audits { get; set; }
}

但我似乎无法通过流畅的映射来允许我插入已在审核中填充AffectedId的Action(1或2)。这就是我在映射对象上的想法,但似乎无法让硬编码的EntityId键正常工作。

public class AuditMap : EntityTypeConfiguration<Audit>
{
    public AuditMap()
    {
        // Primary Key
        this.HasKey(t => t.Id);


        this.HasOptional(t => t.Action1)
            .WithMany(t => t.Audits)
            .HasForeignKey(t => new {EntityId = 3, AffectedId = t.Id});

        this.HasOptional(t => t.Action2)
            .WithMany(t => t.Audits)
            .HasForeignKey(t => new {EntityId = 5, AffectedId = t.Id});
    }
}

public class Action1 : EntityTypeConfiguration<Action1>
{
    public Action1Map()
    {
        // Primary Key
        this.HasKey(t => t.Id);
    }
}

public class Action2 : EntityTypeConfiguration<Action2>
{
    public Action2Map()
    {
        // Primary Key
        this.HasKey(t => t.Id);
    }
}

有关如何更改C#而不是SQL的任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

映射不会处理此方案,但您可以修改域模型和业务规则来执行此操作。拥有EntityId的魔法值枚举将会更好。

public class Audit
{
    public int Id { get; set; }
    public int EntityId { get; set; }
    public string AffectedId { get; set; }       
    public string NewValue { get; set; }
}

public abstract class AuditableAction{

    public AuditableAction() {this.Audits = new List<Audit>();}

    public int Id { get; set; }
    public string Desc { get; set; }
    public virtual IList<Audit> Audits { get; set; }

    public void Audit(string description){
        this.Audits.Add(new Audit(){
            EntityId = this.GetEntityId(),
            AffectedId = this.Id,
            NewValue = description
        });
    }

    public abstract int GetEntityId();
}

public class Action1 : AuditableAction
{
    public override int GetEntityId(){
        return AuditCode.Magic3.GetHashCode();
    }
}   

public class Action2 : AuditableAction
{
    public override int GetEntityId(){
        return AuditCode.Magic5.GetHashCode();
    }
}

public enum AuditCode{
    Magic3 = 3,
    Magic5 = 5
}

基于GitHub代码转储的用法

using (var context = new yunoworkContext())
{
    var a = new Action1() {Id = 4, Desc = Guid.NewGuid().ToString()};

    // This audit should insert an audit with EntityId = 3 and AffectedId = {primary key of the assiciated Action1}
    a.Audit("Console!");
    context.SaveChanges();
}