相关疑难解决方法(0)

实体框架只读集合

考虑一个客户,公司,员工等具有ContactInfo属性的域,该属性又包含一组地址,电话,电子邮件等等......

这是我的缩写ContactInfo:

public class ContactInfo : Entity<int>
{
    public ContactInfo()
    {
        Addresses = new HashSet<Address>();          
    }

    public virtual ISet<Address> Addresses { get ; private set; }

    public Address PrimaryAddress
    {
        get { return Addresses.FirstOrDefault(a => a.IsPrimary); }
    }

    public bool AddAddress(Address address)
    {
        // insure there is only one primary address in collection
        if (address.IsPrimary)
        {                  
            if (PrimaryAddress != null)
            {
                PrimaryAddress.IsPrimary = false;
            }
        }
        else
        {
            // make sure the only address in collection is primary
            if (!Addresses.Any()) …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework readonly-collection ef-code-first

18
推荐指数
2
解决办法
5687
查看次数

实体框架通过包含对象,多对多

我很好奇是否可以通过包含对象映射中间表.

public class Subscriber : IEntity
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    private ChannelList _subscribedList { get; set; }
    public int NumSubscribedChannels { get { return _subscribedList.Count(); } }
}

public class HelpChannel : IEntity
{
    [Key]
    public int Id { get; set; }
    public string name { get; set; }
    public string category { get; set; }
    public int group { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我需要有一个订阅者表,频道表和一个中间表来将订阅者链接到他/她的频道.

是否可以将ChannelList对象中的列表映射到订阅者模型?

我认为这可能是不可能的,我需要有一个私人列表供EF映射.但我不确定EF是否会为私有变量做这件事.会吗?

我希望这样做是因为如果必须公开来维护封装.

c# asp.net-mvc entity-framework

14
推荐指数
1
解决办法
2万
查看次数