简体   繁体   English

实体框架多表关系

[英]Entity Framework Multi Table Relation

I have three tables 我有三张桌子

EVENT - PERSON - COMPANY

I need to have a relation many-to-many using those tables. 我需要使用这些表进行多对多关系。 An event can have one or more "clients",which can be either person or company. 一个事件可以有一个或多个“客户”,可以是个人或公司。 Normally, using no ORM, using sql , it would be something like : 通常,不使用ORM或使用sql,它将类似于:

EVENT
----
id
name

CLIENTEVENT
-----------
id 
clientid
clienttype -- person or company


PERSON
-----------
id 
name
lastname
...


COMPANY
-------
id
name

How does this approach can be replicated using entity framework? 如何使用实体框架复制这种方法? I am pretty new using EF so I would appreciate all help you can give me.I am using repository pattern, following this approach http://www.codeproject.com/Articles/838097/CRUD-Operations-Using-the-Generic-Repository-Pat . 我是使用EF的新手,因此非常感谢您能提供的所有帮助。我正在使用存储库模式,遵循这种方法http://www.codeproject.com/Articles/838097/CRUD-Operations-Using-the-Generic- Repository-Pat

I advice you instead of clientid and clienttype make another two columns: personID and companyID. 我建议您不要在clientid和clienttype中添加另外两列:personID和companyID。 Models will look this way: 模型将如下所示:

public class Event     
{
    public int ID { get; set; }
    public string name { get; set; }

    public virtual ICollection<CLIENTEVENT> links { get; set; }
}

public class Company
{
    public int ID { get; set; }
    public string name { get; set; }

    public virtual ICollection<CLIENTEVENT> links { get; set; }
}

public class PERSON
{
    public int ID { get; set; }
    public string name { get; set; }
    public string lastname { get; set; }

    public virtual ICollection<CLIENTEVENT> links { get; set; }
}

public class CLIENTEVENT
{
    public int ID { get; set; }

    public virtual PERSON person { get; set; }
    public int? personID { get; set; }

    public virtual Company company { get; set; }
    public int? companyID { get; set; }

    public virtual Event event1 { get; set; }
    public int? event1ID { get; set; }
}

With EF Code First, you don't have to deal with joining tables like ClientEvent. 使用EF Code First,您不必处理诸如ClientEvent之类的联接表。 You can simply write: 您可以简单地写:

public class Event     
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Company> Companies { get; set; }

    public virtual ICollection<Person> Persons { get; set; }
}

public class Company
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<Event> Events { get; set; }
}

public class Person
{
    public int ID { get; set; }

    public string Name { get; set; }

    public string Lastname { get; set; }

    public virtual ICollection<Event> Events { get; set; }
}

Try this code, you'll see EF creating two linking tables (EventPerson and EventCompany). 尝试这段代码,您将看到EF创建了两个链接表(EventPerson和EventCompany)。 Now, the client type is the collection you read ( someEvent.Persons or someEvent.Companies ), you can also easily get the events linked to a specific Person or a Company. 现在,客户端类型是您阅读的集合( someEvent.PersonssomeEvent.Companies ),您还可以轻松地将事件链接到特定的Person或Company。

About many-to-many with EF . 关于EF的多对多

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

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