可以使用 EF5 完成吗?
X
有很多Y
Y
有一个X
(一对多的关系)
但是,Y
第二个X
与第一个无关X
这可能吗?
public class X
{
public int Id { get; set; }
public List<Y> Ys { get; set; }
}
public class Y
{
public int Id { get; set; }
public int id_X { get; set; }
[ForeignKey("id_X")]
public X X { get; set; }
public int id_X2 { get; set; }
[ForeignKey("id_X2")]
public X X2 { get; set; }
}
不工作。
代码看起来像这样。
class Program
{
static void Main(string[] args)
{
using (var context = new context())
{
var x = context.X.FirstOrDefault();
}
}
}
public class context : DbContext
{
public context()
: base(@"server=localhost\ALTAIRNOTESTI; uid=sa; pwd=13509; database=DDD")
{
}
public DbSet<X> X { get; set; }
public DbSet<Y> Y { get; set; }
}
public class X
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<Y> Ys { get; set; }
}
public class Y
{
[Required, Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int id_X { get; set; }
[ForeignKey("id_X")]
[InverseProperty("Ys")]
public X X { get; set; }
public int id_X2 { get; set; }
[ForeignKey("id_X2")]
public X X2 { get; set; }
}