EF4.3创建一对一关系失败 [英] EF4.3 create one to one or zero relationship failed
问题描述
我使用EF4.3创建1到1 ... 0关系,但它抛出
的例外操作失败,因为表'TestAs'上已经存在名称为'IX_id'
的索引或统计信息
代码如下
命名空间ConsoleApplication1
{
类程序
{
static void Main string [] args)
{
using(myContext context = new myContext())
{
TestA tA = new TestA();
TestB tB = new TestB();
TestC tC = new TestC();
context.testA.Add(tA);
context.testB.Add(tB);
context.testC.Add(tC);
context.SaveChanges();
}
}
}
class TestA
{
public int id {get;组; }
// public TestB NavB {get;组; }
// public TestC NavC {get;组; }
}
class TestB
{
public int id {get;组; }
public TestA NavA {get;组; }
}
class TestC
{
public int id {get;组; }
public TestA NavA {get;组; }
}
class myContext:DbContext
{
public DbSet< TestA> testA {get;组; }
public DbSet< TestB> testB {get;组; }
public DbSet&Test;> testC {get;组;
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< TestB>()。HasOptional(x => x.NavA).WithRequired();
modelBuilder.Entity< TestC>()。HasOptional(x => x.NavA).WithRequired();
}
}
}
任何人都可以帮助? p>
在 OnModelCreating $中替换
WithRequired
c $ c>方法由 WithOptionalPrincipal
:
protected override void OnModelCreating DbModelBuilder modelBuilder)
{
modelBuilder.Entity< TestB>()。HasOptional(x => x.NavA).WithOptionalPrincipal();
modelBuilder.Entity< TestC>()。HasOptional(x => x.NavA).WithOptionalPrincipal();
}
(如果A将是您将使用的主要实体 WithOptionalDependent
。)
编辑
在您的意见之后,我认为看到添加两个类TestD和TestE的效果会很有趣,给出了两个导航属性TestD和TestE,并在您的模型中执行此操作:
modelBuilder.Entity< TestB>()。HasOptional(x => x.NavA).WithOptionalPrincipal();
modelBuilder.Entity< TestC>()。HasOptional(x => x.NavA).WithOptionalPrincipal();
modelBuilder.Entity< TestA>()。HasRequired(x => x.NavD);
modelBuilder.Entity< TestA>()。HasRequired(x => x.NavE);表B现在有四个外键:B和C(可空),D和E(不可空)。我认为后者是你想要的。
I use EF4.3 to create 1 to 1...0 relationship, but it throw an exception of
"The operation failed because an index or statistics with name 'IX_id'
already exists on table 'TestAs'"
The code as below
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (myContext context = new myContext())
{
TestA tA = new TestA();
TestB tB = new TestB();
TestC tC = new TestC();
context.testA.Add(tA);
context.testB.Add(tB);
context.testC.Add(tC);
context.SaveChanges();
}
}
}
class TestA
{
public int id { get; set; }
//public TestB NavB { get; set; }
//public TestC NavC { get; set; }
}
class TestB
{
public int id { get; set; }
public TestA NavA { get; set; }
}
class TestC
{
public int id { get; set; }
public TestA NavA { get; set; }
}
class myContext : DbContext
{
public DbSet<TestA> testA { get; set; }
public DbSet<TestB> testB { get; set; }
public DbSet<TestC> testC { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestB>().HasOptional(x => x.NavA).WithRequired();
modelBuilder.Entity<TestC>().HasOptional(x => x.NavA).WithRequired();
}
}
}
Anyone can help?
解决方案 Replace WithRequired
in your OnModelCreating
method by WithOptionalPrincipal
:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TestB>().HasOptional(x => x.NavA).WithOptionalPrincipal();
modelBuilder.Entity<TestC>().HasOptional(x => x.NavA).WithOptionalPrincipal();
}
(If A would be the principal entity you'd use WithOptionalDependent
.)
EDIT
After your comments I think it would be interesting to see the effect of adding two classes TestD and TestE, giving A two navigation properties TestD and TestE and do this in your model:
modelBuilder.Entity<TestB>().HasOptional(x => x.NavA).WithOptionalPrincipal();
modelBuilder.Entity<TestC>().HasOptional(x => x.NavA).WithOptionalPrincipal();
modelBuilder.Entity<TestA>().HasRequired(x => x.NavD);
modelBuilder.Entity<TestA>().HasRequired(x => x.NavE);
Table A now has four foreign keys: to B and C (nullable), to D and E (not nullable). I think the latter is what you want.
这篇关于EF4.3创建一对一关系失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!