EntityFramework FluentAPI 映射问题

发布于 2024-10-08 21:14:45 字数 2635 浏览 1 评论 0原文

我有以下代码:

namespace DynamicAssembly {
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Objects;
    using System.Data.EntityClient;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity.ModelConfiguration;
    using System.Data.Entity.Infrastructure;


    [Table("eElementDef1")]
    public class ElementDef1  {

          public int pID { get; set; }
              public virtual ElementDef2  Pointer_EntityDef2 { get; set; }
              public virtual ElementDef1  Poniter_EntityDef1 { get; set; }

        public ElementDef1() {
        }
    }

    [Table("eElementDef2")]
    public class ElementDef2 {


          public int pID { get; set; }
          public String  Name { get; set; }

        public ElementDef2() {
        }
    }

    public class Context : System.Data.Entity.DbContext {

          public DbSet<ElementDef1>  ElementDef1 
            { 
               get;set;
            }
          public DbSet<ElementDef2>  ElementDef2 
            { 
               get;set;
            }

      protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
                  modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID);

modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2)
                                                                        .WithMany()
                                                                        .IsIndependent()
                                                                        .Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2")); 


modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1)
                                                                        .WithMany()
                                                                        .IsIndependent()
                                                                        .Map(m => m.MapKey(p => p.pID, "Poniter_EntityDef1")); 

          modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID);



        }

        public Context() : 
                base("DynamicDefinitionConnection") {
        }
    }

}

问题出在这个映射上 modelBuilder.Entity().HasRequired(p => p.Pointer_EntityDef2) .WithMany() .IsIndependent() .Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2"));

数据库不遵守约定,FK 不是 Pointer_EntityDef2pId 是否有可能在我的类中只有 1 个属性(EntityDef2 类型的 Pointer_EntityDef2),而不是 id 的 1 个属性和对象的 1 个属性(这个可以正常工作),并且属性具有 FK 名称?映射应该是什么样子?

I have the following code:

namespace DynamicAssembly {
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Objects;
    using System.Data.EntityClient;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity.ModelConfiguration;
    using System.Data.Entity.Infrastructure;


    [Table("eElementDef1")]
    public class ElementDef1  {

          public int pID { get; set; }
              public virtual ElementDef2  Pointer_EntityDef2 { get; set; }
              public virtual ElementDef1  Poniter_EntityDef1 { get; set; }

        public ElementDef1() {
        }
    }

    [Table("eElementDef2")]
    public class ElementDef2 {


          public int pID { get; set; }
          public String  Name { get; set; }

        public ElementDef2() {
        }
    }

    public class Context : System.Data.Entity.DbContext {

          public DbSet<ElementDef1>  ElementDef1 
            { 
               get;set;
            }
          public DbSet<ElementDef2>  ElementDef2 
            { 
               get;set;
            }

      protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
                  modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID);

modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2)
                                                                        .WithMany()
                                                                        .IsIndependent()
                                                                        .Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2")); 


modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1)
                                                                        .WithMany()
                                                                        .IsIndependent()
                                                                        .Map(m => m.MapKey(p => p.pID, "Poniter_EntityDef1")); 

          modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID);



        }

        public Context() : 
                base("DynamicDefinitionConnection") {
        }
    }

}

the problem is with this mapping
modelBuilder.Entity().HasRequired(p => p.Pointer_EntityDef2)
.WithMany()
.IsIndependent()
.Map(m => m.MapKey(p => p.pID, "Pointer_EntityDef2"));

The db does not respect de convention, the FK is not Pointer_EntityDef2pId
Is it possible that in my class to have only 1 property (Pointer_EntityDef2 of type EntityDef2) instead of 1 property for the id and one for the object (this one works ok), and the property to have the FK name? And how should the mapping look like?

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

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

发布评论

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

评论(1

伤痕我心 2024-10-15 21:14:45

这个流畅的 API 为您提供了所需的模式(无需在 IsIndependent() 之后调用 Map()):

[Table("eElementDef1")]
public class ElementDef1
{
    public int pID { get; set; }
    public virtual ElementDef2 Pointer_EntityDef2 { get; set; }
    public virtual ElementDef1 Poniter_EntityDef1 { get; set; }
}

[Table("eElementDef2")]
public class ElementDef2
{
    public int pID { get; set; }
    public String Name { get; set; }
}

public class StackoverflowContext : DbContext
{
    public DbSet<ElementDef1> ElementDef1s { get; set; }
    public DbSet<ElementDef2> ElementDef2s { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID);
      modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID);

      modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2)
                                        .WithMany()
                                        .IsIndependent();

      modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1)
                                        .WithMany()
                                        .IsIndependent();
    }
}

This fluent API gives you the desired schema (no need to call Map() after IsIndependent()):

[Table("eElementDef1")]
public class ElementDef1
{
    public int pID { get; set; }
    public virtual ElementDef2 Pointer_EntityDef2 { get; set; }
    public virtual ElementDef1 Poniter_EntityDef1 { get; set; }
}

[Table("eElementDef2")]
public class ElementDef2
{
    public int pID { get; set; }
    public String Name { get; set; }
}

public class StackoverflowContext : DbContext
{
    public DbSet<ElementDef1> ElementDef1s { get; set; }
    public DbSet<ElementDef2> ElementDef2s { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
      modelBuilder.Entity<ElementDef1>().HasKey(c => c.pID);
      modelBuilder.Entity<ElementDef2>().HasKey(c => c.pID);

      modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Pointer_EntityDef2)
                                        .WithMany()
                                        .IsIndependent();

      modelBuilder.Entity<ElementDef1>().HasRequired(p => p.Poniter_EntityDef1)
                                        .WithMany()
                                        .IsIndependent();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文