EF FluentAPI 0..1 到 * 映射

发布于 2024-10-08 21:40:56 字数 1169 浏览 6 评论 0原文

如何在 EF 4.0 FluentAPI CTP5 中将 0..1 映射到 * 关系? 我不断收到此错误

因为从属角色中的所有属性都是不可为空的, 主要角色的重数必须为“1”。

我不知道如何准确地修复它。

我的代码看起来像这样

    public class Child{
        public int pID { get; set; }
        public Parent Parent_Object{ get; set; } 
        public int Parent{ get; set; } 

        public Child() {
        }
    }


    public class Parent {
        public int pID { get; set; }
        public List<Child>  Children { get; set; }

        public Parent () {
        }
    }

对于映射,代码看起来像这样

modelBuilder.Entity<Child>().HasKey(c=> c.pID);  
modelBuilder.Entity<Parent>().HasKey(c=> c.pID); 
modelBuilder.Entity<Child>().HasOptional(c=> c.Parent_Object)
                            .WithMany(p => p.Children)
                            .HasForeignKey(p => p.Parent);

也可以只

public Parent Parent{ get; set; } 

代替

public Parent Parent_Object{ get; set; } 
public int Parent{ get; set; } 

在数据库中,FKfield 被命名为“Parent”而不是“ParentpID”。在这种情况下,映射应该是什么样的?

How do I map a 0..1 to * relation in EF 4.0 FluentAPI CTP5?
I keep geting this error

Because all of the properties in the Dependent Role are non-nullable,
multiplicity of the Principal Role must be '1'.

And I do not know how exactly to fix it..

My code looks like this

    public class Child{
        public int pID { get; set; }
        public Parent Parent_Object{ get; set; } 
        public int Parent{ get; set; } 

        public Child() {
        }
    }


    public class Parent {
        public int pID { get; set; }
        public List<Child>  Children { get; set; }

        public Parent () {
        }
    }

For mapping the code looks like this

modelBuilder.Entity<Child>().HasKey(c=> c.pID);  
modelBuilder.Entity<Parent>().HasKey(c=> c.pID); 
modelBuilder.Entity<Child>().HasOptional(c=> c.Parent_Object)
                            .WithMany(p => p.Children)
                            .HasForeignKey(p => p.Parent);

Also is it possible to have only

public Parent Parent{ get; set; } 

instead of

public Parent Parent_Object{ get; set; } 
public int Parent{ get; set; } 

In the database the FKfield is named "Parent" not "ParentpID". In this case how should the mapping look like?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴骨ら 2024-10-15 21:40:56

您可以简单地从映射中删除外键列,一切都会正常工作:

  public class Child{
    public int pID { get; set; }
    public Parent Parent_Object{ get; set; } 

    public Child() { }
  }


  public class Parent {
    public int pID { get; set; }
    public List  Children { get; set; }

    public Parent () { }
  }

  public class Context : DbContext {

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {

      modelBuilder.Entity().HasKey(c => c.pID);
      modelBuilder.Entity().HasKey(c => c.pID);  
      modelBuilder.Entity().HasOptional(c => c.Parent_Object).WithMany(p => p.Children);
    }

    public DbSet Parents { get; set; }
    public DbSet Childs { get; set; }
  }

作为替代方案,您可以使用可空 int Parent 属性,如下所示:

public int? ParentId { get; set; }  

在这种情况下,您的初始代码也将是正确的。

You can simply remove Foreign Key Column from the mapping and everything will work fine:

  public class Child{
    public int pID { get; set; }
    public Parent Parent_Object{ get; set; } 

    public Child() { }
  }


  public class Parent {
    public int pID { get; set; }
    public List  Children { get; set; }

    public Parent () { }
  }

  public class Context : DbContext {

    protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder) {

      modelBuilder.Entity().HasKey(c => c.pID);
      modelBuilder.Entity().HasKey(c => c.pID);  
      modelBuilder.Entity().HasOptional(c => c.Parent_Object).WithMany(p => p.Children);
    }

    public DbSet Parents { get; set; }
    public DbSet Childs { get; set; }
  }

As an alternative, you can use the nullable int Parent property, like this:

public int? ParentId { get; set; }  

In this case your initial code will be correct as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文