将对象列表作为外键 [英] Have a list of objects as a foreign key

查看:146
本文介绍了将对象列表作为外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个类:

 public class Event
    {
        public int EventId { get; set; }
        public string EventName { get; set; }

    }

public class Dog
    {
        public int DogId { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

我想为我的事件 -class可以附加一个 dog 的列表。
如何让我的活动类了解我想要它能够包含一个狗列表?
我正在使用实体框架。谢谢!

I would like for my event-class to be able to have a list of dog attached to it. How can I make my event-class understand that I want it to be able to contain a list of dogs? I am using entity framework. Thank you!

推荐答案

假设您使用的是Code-First:

Assuming you're using Code-First:

public class Event
{
    public int EventId { get; set; }
    public string EventName { get; set; }

    public virtual ICollection<Dog> Dogs { get; set; }
}

public class Dog
{
    public int DogId { get; set; }

    [ForeignKey("Event")]
    public int EventID { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }

    public virtual Event Event { get; set; }
}

这篇关于将对象列表作为外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆