如何在“实体框架代码优先"中设置0 .. *关系? [英] How to set a 0..* relationship in Entity Framework Code First?

查看:128
本文介绍了如何在“实体框架代码优先"中设置0 .. *关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类的下一个代码:

I have the next code for two classes:

public class Object
{
    public int ObjectID { get; set; }

    public int Object2ID { get; set; }
    public virtual Object2 Object2 { get; set; }
}

public class Object2
{
    public int Object2ID { get; set; }

    public virtual ICollection<Object> Objects { get; set; }
}

我知道使用Entity Framework,这将创建一对多关系,但是我想知道的是如何将其转换为零对多关系.

I know that with Entity Framework, this will create a one-to-many relationship, but what I want to know, is how to transform this to a zero-to-many relationship.

我是Entity Framework的新手,我找不到任何直接的答案.

I'm new to Entity Framework and I couldn't find any direct answer around.

推荐答案

对于Entity Framework中的0对多关系,外键可以为空.

For a 0-to-many relationship in Entity Framework, have the foreign key be nullable.

public int? Object2ID { get; set; }

这篇关于如何在“实体框架代码优先"中设置0 .. *关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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