EF4.3创建一对一关系失败

编程入门 行业动态 更新时间:2024-10-24 15:26:34
本文介绍了EF4.3创建一对一关系失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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 方法由 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创建一对一关系失败

本文发布于:2023-11-14 13:53:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587395.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!