Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 多对多关系_Entity Framework_Code First - Fatal编程技术网

Entity framework 多对多关系

Entity framework 多对多关系,entity-framework,code-first,Entity Framework,Code First,我有两个实体对象: public class Contact { public int ContactID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public ICollection<Fri

我有两个实体对象:

   public class Contact
    {
        public int ContactID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }

        public ICollection<Friend> Friends { get; set; }

    }
其中,ContactId1映射回一个ContactID,ContactId2映射回另一个ContactID


我该如何确保生成的是正确的关系?

我想你需要两排朋友,两排是联系人,对吗?如果是,您需要创建两个friend实例。默认情况下,实体框架代码首先识别 按属性名称设置关键关系。因此,在Friend类中,属性应该 命名ContactId,因为实体为names Contact

public class Contact
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public ICollection<Friend> Friends { get; set; }

}

public class Friend
{
    public int ContactId { get; set; }
    public DateTime DateCreated { get; set; }
}
公共类联系人
{
public int ContactID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串电子邮件{get;set;}
公共ICollection好友{get;set;}
}
公课朋友
{
public int ContactId{get;set;}
public DateTime DateCreated{get;set;}
}
编辑 如果你想要一个朋友有很多联系人,你需要一个关系表

public class Contact
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public ICollection<Friend> Friends { get; set; }

}

public class Friend
{
    public int FriendId { get; set; }
    public DateTime DateCreated { get; set; }
}

public class FriendContactRel 
{
    [Key, Column(Order = 0)]
    public int ContactID { get; set; }
    [Key, Column(Order = 1)]
    public int FriendId { get; set; }
}
公共类联系人
{
public int ContactID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串电子邮件{get;set;}
公共ICollection好友{get;set;}
}
公课朋友
{
public int-FriendId{get;set;}
public DateTime DateCreated{get;set;}
}
公共类FriendContactRel
{
[键,列(顺序=0)]
public int ContactID{get;set;}
[键,列(顺序=1)]
public int-FriendId{get;set;}
}

您需要扩展您的
朋友
课程:

public class Friend
{
    [Key, Column(Order = 0), ForeignKey("Contact1")]
    public int ContactId1 { get; set; }
    public Contact Contact1 { get; set; }

    [Key, Column(Order = 1), ForeignKey("Contact2")]
    public int ContactId2 { get; set; }
    public Contact Contact2 { get; set; }

    public DateTime DateCreated { get; set; }
}
并在
联系人
中的
朋友
集合中添加一个属性:

[InverseProperty("Contact1")]
public ICollection<Friend> Friends { get; set; }
Eit

上面的映射不起作用,因为
朋友
联系人
有两个必需的关系。默认情况下,对于所需的一对多关系,EF将启用级联删除,这不允许同时针对两个关系。我们需要使用Fluent API重写映射以禁用级联删除,因为这在数据注释中是不可能的:

public class Contact
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public ICollection<Friend> Friends { get; set; }
}

public class Friend
{
    public int ContactID1 { get; set; }
    public Contact Contact1 { get; set; }

    public int ContactID2 { get; set; }
    public Contact Contact2 { get; set; }

    public DateTime DateCreated { get; set; }
}

dknaack可能重复,如果我希望一个朋友行与两个联系人关联,该怎么办?若我在设计数据库,那个么ContactId1将指向联系人表,ContactId2也将指向联系人表。这在我的解决方案中是可能的。我的解决方案说。。。它不可能有两行同一个朋友和相同的联系是可能的。你可以有几个朋友拥有相同的联系人,也可以有几个联系人拥有相同的朋友。这对manyI来说是非常重要的,他可以看到这是如何运作的。但这会创建三个数据库表。我想我可以用两张桌子。一个保存联系人,另一个保存朋友,其中朋友表行由两个不同的ContactId值(ContactId1和ContactId2)组成感谢Slauma--但我在运行应用程序时遇到一个错误--未处理的异常:System.InvalidOperationException:数据库创建成功,但数据库对象的创建没有成功。有关详细信息,请参阅内部异常。-->System.Data.SqlServerCe.SqlCeException:引用关系将导致不允许的循环引用。[约束名称=联系朋友]。。想法?@ek_ny:请看上面我的编辑。我不确定它是否能解决问题,请告诉我它是否有效。
DateTime today = DateTime.Now.Date;
IEnumerable<Contact> friendContacts = context.Contacts
    .Where(c => c.ContactId == givenId)
    .Select(c => c.Friends
        .Where(f => f.DateCreated < today)
        .Select(f => f.Contact2))
    .SingleOrDefault();
public class Contact
{
    public int ContactID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    public ICollection<Friend> Friends { get; set; }
}

public class Friend
{
    public int ContactID1 { get; set; }
    public Contact Contact1 { get; set; }

    public int ContactID2 { get; set; }
    public Contact Contact2 { get; set; }

    public DateTime DateCreated { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Friend>()
        .HasKey(f => new { f.ContactID1, f.ContactID2 });

    modelBuilder.Entity<Friend>()
        .HasRequired(f => f.Contact1)
        .WithMany(c => c.Friends)
        .HasForeignKey(f => f.ContactID1);

    modelBuilder.Entity<Friend>()
        .HasRequired(f => f.Contact2)
        .WithMany()
        .HasForeignKey(f => f.ContactID2)
        .WillCascadeOnDelete(false);  // <- Important
}