小编Mar*_*tin的帖子

实体框架,代码优先,更新与独立协会的"一对多"关系

找到下面描述的场景的解决方案花了我太长时间.看似简单的事情应该被证明是相当困难的.问题是:

使用实体框架4.1(代码优先方法)和"独立关联"如何在"分离"场景(在我的案例中为Asp.Net)中为现有的"多对一"关系分配不同的结尾.

该模型:

我意识到使用ForeignKey关系而不是独立关联可能是一个选项,但我喜欢在我的Pocos中没有ForeignKey实现.

客户有一个或多个目标:

    public class Customer:Person
{
    public string Number { get; set; }
    public string NameContactPerson { get; set; }
    private ICollection<Target> _targets;

    // Independent Association
    public virtual ICollection<Target> Targets
    {
        get { return _targets ?? (_targets = new Collection<Target>()); }
        set { _targets = value; }
    }
}
Run Code Online (Sandbox Code Playgroud)

目标有一个客户:

    public class Target:EntityBase
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string Note { get; set; }
    public virtual …
Run Code Online (Sandbox Code Playgroud)

entity-framework associations code-first dbcontext

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