简体   繁体   English

使用Fluent API在实体框架中创建一对多关系

[英]Creating one to many relationship in Entity Framework using Fluent API

I have the following POCO Classes: 我有以下POCO类别:

public class Client
{
    public string ClientId { get; set; }
    public string CompanyId { get; set; }
    public string LastName { get; set; }
    public string FirstName { get; set; }

    [ForeignKey("AccountId")]
    public virtual ICollection<Note> Notes { get; set; } 
}

public class Note
{
    public decimal NoteId { get; set; } 
    public string AccountId { get; set; }
    public string NoteValue { get; set; }

    [ForeignKey("ClientId")]
    public virtual Client Client { get; set; }
}

and the following mapping classes: 以及以下映射类:

    public ClientMap(string schema)
    {
        ToTable("CLIENTS", schema);

        // Primary Key
        HasKey(t => t.ClientId);

        // Properties
        Property(t => t.ClientId)
            .HasColumnName("CLIENT_ID")
            .IsRequired()
            .HasMaxLength(16);

        Property(t => t.CompanyId)
            .HasColumnName("COMPANY_ID")
            .IsRequired()
            .HasMaxLength(16);

        Property(t => t.LastName)
            .HasColumnName("LAST_NAME")
            .IsRequired()
            .HasMaxLength(50);

        Property(t => t.FirstName)
            .HasColumnName("FIRST_NAME")
            .IsRequired()
            .HasMaxLength(50);
    }

    public NoteMap(string schema)
    {
        ToTable("NOTES", schema);

        // Primary Key
        HasKey(t => t.NoteId);

        // Properties
        Property(t => t.NoteId)
            .HasColumnName("NOTE_ID")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        Property(t => t.AccountId)
            .HasColumnName("ACCOUNT_ID")
            .IsRequired()
            .HasMaxLength(16);

        Property(t => t.NoteValue)
            .HasColumnName("NOTE")
            .HasMaxLength(4000);
    }

In this model (Using Fluent API), there is a one to many relationship between clients and notes. 在此模型(使用Fluent API)中,客户端和注释之间存在一对多关系。 ClientID is the PK in clients and NoteId is the PK in Notes. ClientID是客户端中的PK, NoteId是Notes中的PK。 There are no foreign keys in the DB. 数据库中没有外键。 ClientId maps to the AccountId in Notes. ClientId映射到Notes中的AccountId

I can not get this to work. 我无法使它正常工作。 When I run it, I can get most of the client data back, but when trying to navigate to a note, I get a Funcation Evaluation Timed out error when trying to look at Notes. 运行它时,我可以获取大部分客户端数据,但是当尝试导航到便笺时,尝试查看便笺时会收到“功能评估超时”错误。 I can not get the relationship between clients and notes to work. 我无法获得客户和工作笔记之间的关系。 Where have I gone wrong? 我哪里出问题了? (I would like to do this using Fluent API) (我想使用Fluent API做到这一点)

Thanks 谢谢

You are trying to create a typical one to many relationship. 您正在尝试创建典型的一对多关系。 First, remove the data annotations that you are using in your model, you don't need them if you are going to use Fluent Api. 首先,删除在模型中使用的数据注释,如果要使用Fluent Api,则不需要它们。 Second, add the ClientId FK property in your Note entity 其次,在您的Note实体中添加ClientId FK属性

public class Note
{
    public decimal NoteId { get; set; } 
    public string AccountId { get; set; }
    public string NoteValue { get; set; }

    //FK property
    public int ClientId{get;set;}

    public virtual Client Client { get; set; }
}

Then, in the constructor of your NoteMap class, add this Fluent Api configuration: 然后,在NoteMap类的构造函数中,添加以下Fluent Api配置:

 HasRequired(n=>n.Client).WithMany(c=>c.Notes).HasForeignKey(n=>n.ClientId);

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

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