简体   繁体   English

在Entity Framework Core中建立关系

[英]Setting up a relationship in Entity Framework Core

I'm trying to set up a one to many relationship in Entity Framework Core using the Fluent API with no success. 我正在尝试使用Fluent API在Entity Framework Core中建立一对多关系,但没有成功。

I have two objects called Message and Source and are defined as 我有两个名为Message和Source的对象,它们被定义为

public class Message
{
    public int ID { get; set; }
    public int FromUserID { get; set; }
    public int ToUserID { get; set; }
    public int SourceID { get; set; }
    public int Priority { get; set; }
    public string Subject { get; set; }
    public string MessageBody { get; set; }
    public DateTime DateTimeCreated { get; set; }
    public DateTime? DateTimeDelivered { get; set; }
    public Source Source { get; set; }
}

public class Source
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ICollection<Message> Messages { get; set; }
}

with one message relating to one source and one source relating to many messages. 一个消息与一个消息源相关,一个消息与许多消息相关。

In my context class I then have the following 在我的上下文类中,我有以下内容

public DbSet<Message> Messages { get; set; }

public DbSet<Source> Sources { get; set; }

and then define the relationship as 然后将关系定义为

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    ...

    modelBuilder.Entity<Message>()
            .HasOne<Source>(m => m.Source)
            .WithMany(s => s.Messages);

}

If I test this with the following 如果我用以下方法测试

var get = context.Messages.Where(m => m.ID == 1).FirstOrDefault();

My problem is I get the data returned for the message OK but for source I'm only getting null. 我的问题是我得到的消息返回的数据确定,但是对于源,我只会得到空值。

I've looked at different tutorials and SOF questions regarding this, but I can't see where I'm going wrong. 我看过与此有关的其他教程和SOF问题,但看不到我要去哪里。

Hope someone can shed some light on this. 希望有人能对此有所启发。

There are several ways to load related data: 有几种加载相关数据的方法:

Eager loading: 渴望加载:

means that the related data is loaded from the database as part of the initial query 表示相关数据是作为初始查询的一部分从数据库中加载的

Explicit loading: 显式加载:

means that the related data is explicitly loaded from the database at a later time 意味着以后会从数据库显式加载相关数据

Lazy loading: 延迟加载:

means that the related data is transparently loaded from the database when the navigation property is accessed 表示访问导航属性时,将从数据库中透明地加载相关数据

See the EF Core docs for more information. 有关更多信息,请参阅EF Core文档

If we go with the lazy loading approach, you can set your options with UseLazyLoadingProxies(): 如果我们采用延迟加载方法,则可以使用UseLazyLoadingProxies()设置选项:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
    .UseLazyLoadingProxies()
    .UseSqlServer(myConnectionString);

...and then make your navigation properties virtual: ...然后将您的导航属性设为虚拟:

public virtual Source Source { get; set; }

and

public virtual ICollection<Message> Messages { get; set; }

Include(msg => msg.Source)添加到查询中,它将强制其加载消息var get = context.Messages.Include(msg => msg.Source).Where(m => m.ID == 1).FirstOrDefault();

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

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