简体   繁体   English

EF6一对一或零关系(Fluent API)

[英]EF6 One to One or Zero Relationship (Fluent API)

I have such classes (simplicified): 我有这样的类(简化):

public class Transaction
{
    public int LocalId { get; set; }
    public int MachineId { get; set; }
    public virtual Machine Machine { get; set; }       
    public int? MoneyId { get; set; }
    public virtual TransactionMoney Money { get; set; }
}

public class Machine
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class TransactionMoney
{
    public int LocalId { get; set; }
    public int MachineId { get; set; }
    public virtual Machine Machine { get; set; }
    public int TransactionId { get; set; }
    public virtual Transaction Transaction { get; set; }
}

I would like to have relationship Transaction 1 <-> 0...1 TransactionMoney where foreign key in Money should be TransactionId and MachineId (connected to transaction's LocalId and MachineId). 我想拥有事务1 <-> 0 ... 1 TransactionMoney的关系,其中Money中的外键应该是TransactionId和MachineId(连接到事务的LocalId和MachineId)。 I need to do this in fluent API. 我需要在流利的API中执行此操作。

What I've tried is: 我试过的是:

    modelBuilder.Entity<Transaction>()
                .HasOptional(t => t.Money)
                .WithRequired(t => t.Transaction)
                .HasForeignKey() <--- there is no such method

and in other side 在另一边

modelBuilder.Entity<TransactionMoney>()
    .HasRequired(t => t.Transaction)
    .WithOptional(t => t.Money)
            .HasForeignKey() <--- there is no such method

You can use something like this 你可以用这样的东西

modelBuilder.Entity<TransactionMoney>()
    .HasRequired(t => t.Transaction)
    .WithOptional(t => t.Money)
    .Map(a => a.MapKey("TransactionId", "MachineId"));


It turns out that the design you are targeting cannot be done in EF. 事实证明,您要针对的设计无法在EF中完成。 The closest I was able to get is as follows. 我能得到的最接近的信息如下。 But before I go, there are some things to note. 但是在我走之前,有一些事情要注意。 MoneyId field is removed from Transaction . MoneyId字段已从Transaction删除。 LocalId field is removed from TransactionMoney . LocalId字段已从TransactionMoney删除。 Foreign key is specified with data annotation. 外键通过数据注释指定。 If any of those is unacceptable, just skip the rest. 如果其中任何一个不可接受,则跳过其余部分。

Entities: 实体:

public class Transaction
{
    public int LocalId { get; set; }
    public int MachineId { get; set; }
    public virtual Machine Machine { get; set; }
    public virtual TransactionMoney Money { get; set; }
}

public class Machine
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Transaction> Transactions { get; set; }
    public virtual ICollection<TransactionMoney> Money { get; set; }
}

public class TransactionMoney
{
    public int MachineId { get; set; }
    public virtual Machine Machine { get; set; }
    public int TransactionId { get; set; }
    [ForeignKey("TransactionId,MachineId")]
    public virtual Transaction Transaction { get; set; }
}

Configuration 组态

modelBuilder.Entity<Transaction>()
    .HasKey(t => new { t.LocalId, t.MachineId })
    .HasRequired(t => t.Machine)
    .WithMany(t => t.Transactions)
    .HasForeignKey(t => t.MachineId);

modelBuilder.Entity<TransactionMoney>()
    .HasRequired(t => t.Machine)
    .WithMany(t => t.Money)
    .HasForeignKey(t => t.MachineId);

modelBuilder.Entity<TransactionMoney>()
    .HasKey(t => new { t.TransactionId, t.MachineId })
    .HasRequired(t => t.Transaction)
    .WithOptional(t => t.Money);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM