实体POCO与EF,与POCO建立关系

时间:2012-10-16 00:22:01

标签: entity-framework poco model-associations

祝你好日子

我希望你能帮助我,然后我会解释我的问题:

这些是我的实体:

< p> http://i.stack.imgur.com/OjZmX.jpg

我需要用POCO创建这样的关系,比如带有SDN的resultadobusquedaofac,为此,我使用了POCO模板

http://i.stack.imgur.com/qYbzy.jpg

然后我自己相信这些实体内的合作关系:

ResultadoBusquedaOfac

public partial class ResultadoBusquedaOfac
{
    #region Primitive Properties

    public virtual int ent_num
    {
        get;
        set;
    }

    public virtual string SDN_Name
    {
        get;
        set;
    }

    public virtual int KeyCliente
    {
        get;
        set;
    }

    public virtual string HayCoincidencia
    {
        get;
        set;
    }

    public virtual string HayCoincidenciaAlias
    {
        get;
        set;
    }

    #endregion

    public virtual ICollection&lt;OFAC_SDN&gt; OFAC_SDN
    {
        get
        {
            if (_oFAC_SDN == null)
            {
                var newCollection = new FixupCollection&lt;OFAC_SDN&gt;();
                newCollection.CollectionChanged += FixupOFAC_SDN;
                _oFAC_SDN = newCollection;
            }
            return _oFAC_SDN;
        }
        set
        {
            if (!ReferenceEquals(_oFAC_SDN, value))
            {
                var previousValue = _oFAC_SDN as FixupCollection&lt;OFAC_SDN&gt;;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupOFAC_SDN;
                }
                _oFAC_SDN = value;
                var newValue = value as FixupCollection&lt;OFAC_SDN&gt;;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupOFAC_SDN;
                }
            }
        }
    }
    private ICollection&lt;OFAC_SDN&gt; _oFAC_SDN;</em></strong>


    private void FixupOFAC_SDN(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (OFAC_SDN item in e.NewItems)
            {
                item.ResultadoBusquedaOfac = this;
            }
        }

        if (e.OldItems != null)
        {
            foreach (OFAC_SDN item in e.OldItems)
            {
                if (ReferenceEquals(item.ResultadoBusquedaOfac, this))
                {
                    item.ResultadoBusquedaOfac = null;
                }
            }
        }
    }
}

} OFAC_SDN

public partial class OFAC_SDN
{
    #region Primitive Properties

   <strong><em> public virtual Nullable&lt;int&gt; id_Ofac
    {
        get { return _id_Ofac; }
        set
        {
            try
            {
                _settingFK = true;
                if (_id_Ofac != value)
                {
                    if (ResultadoBusquedaOfac != null &amp;&amp; ResultadoBusquedaOfac.ent_num != value)
                    {
                        ResultadoBusquedaOfac = null;
                    }
                    _id_Ofac = value;
                }
            }
            finally
            {
                _settingFK = false;
            }
        }
    }
    private Nullable&lt;int&gt; _id_Ofac;</em></strong>

    public virtual int ent_num
    {
        get;
        set;
    }

    public virtual string SDN_Name
    {
        get;
        set;
    }

    public virtual string SDN_Type
    {
        get;
        set;
    }

    public virtual string Program
    {
        get;
        set;
    }

    public virtual string Title
    {
        get;
        set;
    }

    public virtual string Call_Sign
    {
        get;
        set;
    }

    public virtual string Vess_type
    {
        get;
        set;
    }

    public virtual string Tonnage
    {
        get;
        set;
    }

    public virtual string GRT
    {
        get;
        set;
    }

    public virtual string Vess_flag
    {
        get;
        set;
    }

    public virtual string Vess_owner
    {
        get;
        set;
    }

    public virtual string Remarks
    {
        get;
        set;
    }

    #endregion
    #region Navigation Properties

    public virtual ResultadoBusquedaOfac ResultadoBusquedaOfac
    {
        get { return _resultadoBusquedaOfac; }
        set
        {
            if (!ReferenceEquals(_resultadoBusquedaOfac, value))
            {
                var previousValue = _resultadoBusquedaOfac;
                _resultadoBusquedaOfac = value;
                FixupResultadoBusquedaOfac(previousValue);
            }
        }
    }
    private ResultadoBusquedaOfac _resultadoBusquedaOfac;</em></strong>

    public virtual ICollection&lt;OFAC_ADD&gt; OFAC_ADD
    {
        get
        {
            if (_oFAC_ADD == null)
            {
                var newCollection = new FixupCollection&lt;OFAC_ADD&gt;();
                newCollection.CollectionChanged += FixupOFAC_ADD;
                _oFAC_ADD = newCollection;
            }
            return _oFAC_ADD;
        }
        set
        {
            if (!ReferenceEquals(_oFAC_ADD, value))
            {
                var previousValue = _oFAC_ADD as FixupCollection&lt;OFAC_ADD&gt;;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupOFAC_ADD;
                }
                _oFAC_ADD = value;
                var newValue = value as FixupCollection&lt;OFAC_ADD&gt;;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupOFAC_ADD;
                }
            }
        }
    }
    private ICollection&lt;OFAC_ADD&gt; _oFAC_ADD;

    public virtual ICollection&lt;OFAC_ALT&gt; OFAC_ALT
    {
        get
        {
            if (_oFAC_ALT == null)
            {
                var newCollection = new FixupCollection&lt;OFAC_ALT&gt;();
                newCollection.CollectionChanged += FixupOFAC_ALT;
                _oFAC_ALT = newCollection;
            }
            return _oFAC_ALT;
        }
        set
        {
            if (!ReferenceEquals(_oFAC_ALT, value))
            {
                var previousValue = _oFAC_ALT as FixupCollection&lt;OFAC_ALT&gt;;
                if (previousValue != null)
                {
                    previousValue.CollectionChanged -= FixupOFAC_ALT;
                }
                _oFAC_ALT = value;
                var newValue = value as FixupCollection&lt;OFAC_ALT&gt;;
                if (newValue != null)
                {
                    newValue.CollectionChanged += FixupOFAC_ALT;
                }
            }
        }
    }
    private ICollection&lt;OFAC_ALT&gt; _oFAC_ALT;

    #endregion

    #region Association Fixup

    private bool _settingFK;

    private void FixupResultadoBusquedaOfac(ResultadoBusquedaOfac previousValue)
    {
        if (previousValue != null &amp;&amp; previousValue.OFAC_SDN.Contains(this))
        {
            previousValue.OFAC_SDN.Remove(this);
        }

        if (ResultadoBusquedaOfac != null)
        {
            if (!ResultadoBusquedaOfac.OFAC_SDN.Contains(this))
            {
                ResultadoBusquedaOfac.OFAC_SDN.Add(this);
            }
            if (id_Ofac != ResultadoBusquedaOfac.ent_num)
            {
                id_Ofac = ResultadoBusquedaOfac.ent_num;
            }
        }
        else if (!_settingFK)
        {
            id_Ofac = null;
        }
     }

    private void FixupOFAC_ADD(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (OFAC_ADD item in e.NewItems)
            {
                item.OFAC_SDN = this;
            }
        }

        if (e.OldItems != null)
        {
            foreach (OFAC_ADD item in e.OldItems)
            {
                if (ReferenceEquals(item.OFAC_SDN, this))
                {
                    item.OFAC_SDN = null;
                }
            }
        }
    }

    private void FixupOFAC_ALT(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null)
        {
            foreach (OFAC_ALT item in e.NewItems)
            {
                item.OFAC_SDN = this;
            }
        }

        if (e.OldItems != null)
        {
            foreach (OFAC_ALT item in e.OldItems)
            {
                if (ReferenceEquals(item.OFAC_SDN, this))
                {
                    item.OFAC_SDN = null;
                }
            }
        }
    }

    #endregion
}

}

现在,当我运行测试查询时:

    公共部分类Form1:表格     {         公共Form1()         {             的InitializeComponent();

        LegitimacionEntities context = new LegitimacionEntities();
        var query = context.ResultadoBusquedaOfac.Include("OFAC_SDN").Where(us =&gt; us.ent_num.Equals(10)).First();

        MessageBox.Show(query.SDN_Name);
    }
}

}

引发以下错误:

指定的包含路径无效。 EntityType'LegitimacionModel.ResultadoBusquedaOfac'没有声明名为'OFAC_SDN'的导航属性。

除此之外,我必须通过dataservice或WCF传递数据,现在我正在做所以首先要证明关系是好的,然后再将其传递给上述人员。

非常感谢你们,我欢迎你的评论......

欢乐时光



0 个答案:

没有答案