EF Code First 0..1至0..1关系 [英] EF Code First 0..1 to 0..1 relationship

查看:56
本文介绍了EF Code First 0..1至0..1关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试与实体之间建立可选关系,但是我在查找正确的语法时遇到了麻烦.其实我需要一个0..1到0..2的关系,但是我猜测一旦我找到了将0..1到0..1的方法,那将是微不足道的.

I'm trying to make an optional relationship between to entities, but I'm having trouble finding the correct syntax. Actually I need a 0..1 to 0..2 relationship, but I'm guessing that once I find the way to do the 0..1 to 0..1 that will be trivial.

简化了我的意思:

class Foo
{
    int Id { get; set; }
    //navigation property
    virtual Bar Bar { get; private set; }
}

class Bar
{
    int Id { get; set; }

    int? LeftFooId { get; set; }
    [ForeignKey("LeftFooId")]
    Foo LeftFoo { get; set; }

    int? RightFooId{ get; set; } 
    [ForeignKey("RightFooId")]
    Foo RightFoo { get; set; }
}

Foo可以连接到零或一个Bar,再也不能连接.条形图可以具有LeftFoo和RightFoo,或者具有一个或两个都不具有.如果Foo的Bar属性没有被Bar引用,则应该为null,并且应包含在Bar引用时引用它的Bar.

A Foo can be connected to zero or one Bar, never more. A Bar can have a LeftFoo and a RightFoo, or one or neither. The Bar property of Foo should be null if it is not referenced by a Bar, and it should contain the Bar that references it when it is referenced by a Bar.

使用Bar上方的代码可以正确地引用Foo,但是EF为Foo表提供了Bar_Id,并且Bar属性始终为null.

With the code above the Bar references the Foo correctly, but EF gives the Foo table a Bar_Id and the Bar property is always null.

我尝试了几种不同的方法来设置这些实体类,并且使用了不同的Fluent API调用来完成这项工作,但是我没有得到想要的结果.

I've tried several different ways to setup these entity classes and I've used differenct Fluent API calls to make this work, but I don't get the wanted results.

推荐答案

尝试一下:

public class Foo
{
    public int Id { get; set; }
    public virtual Bar Bar { get; set; }
}

public class Bar
{
    public int Id {get; set;}
    public virtual Foo LeftFoo {get;set;}
    public virtual Foo RightFoo {get;set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Bar>()
                .HasOptiona(b => b.LeftFoo)
                .WithOptionalDependent()
                .Map(k = k.MapKey("LeftFooId"));

    modelBuilder.Entity<Bar>()
                .HasOptional(b => b.RightFoo)
                .WithOptionalDependent()
                .Map(k => k.MapKey("RightFooId"));
    ...
}

结果

Result

编辑种子

Edit for seeding

我会做以下事情,没有测试代码,但是应该可以工作:

I would do the following, didn't test the code, but it should work:

var bars = new List<Bar>();
   bars.Add(new Bar());
   bars.Add(new Bar());
...//this means as many as you need/want
   bars.ForEach(b => context.Bars.AddOrUpdate(b));
context.SaveChanges();

var leftFoos = new List<Foo>();
   leftFoos.Add(new Foo());
   leftFoos.Add(new Foo());
...//this means as many as you need/want
   leftFoos.ForEach(f => context.Foos.AddOrUpdate(f));
context.SaveChanges();

var rightFoos = new List<Foo>();
   rightFoos.Add(new Foo());
   rightFoos.Add(new Foo());
...//this means as many as you need/want
   rightFoos.Foreach(f => context.Foos.AddOrUpdate(f));
context.SaveChanges();

int i=0;
foreach(var bar in bars)
{
   bar.LeftFoo = leftFoos.ElementAt(i);
   bar.RightFoo = rightFoos.ElementAt(i);
   i++;
}
context.SaveChanges();

为简单起见, bars leftFoos rightFoos 具有相同数量的元素.

For simplicity bars, leftFoos and rightFoos have the same number of elements.

这篇关于EF Code First 0..1至0..1关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆