NPoco。多对多

时间:2019-04-25 13:42:59

标签: c# orm many-to-many npoco

我的数据库和C#模型中都有两个表:

public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
}


public class Customer
{
    public int Id { get; set; }  
    public string FirstName { get; set; }  
    public string LastName { get; set; }
}

我还有一个表,该表将AddressCustomer表与列连接起来: Id CustomerId AddressId

如何使用NPoco在表之间实现多对多关系?

1 个答案:

答案 0 :(得分:0)

使用此示例:

BindingResult

并在模型创建时进行配置:

public class Address
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public virtual ICollection<CustomerAddress> CustomerAddresses{ get; set; }
}

public class Customer
{
    public int Id { get; set; }  
    public string FirstName { get; set; }  
    public string LastName { get; set; }
    public virtual ICollection<CustomerAddress> CustomerAddresses{ get; set; }
}

public class CustomerAddress
{
    public int CustomerId{ get; set; }
    public int AddressId{ get; set; }

    public virtual Address Address{ get; set; }
    public virtual Customer Customer{ get; set; }
}
相关问题