EntityFramework FluentAPI 映射問題 (EntityFramework FluentAPI mapping issue)


問題描述

EntityFramework FluentAPI 映射問題 (EntityFramework FluentAPI mapping issue)

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?


參考解法

方法 1:

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();
    }
}

(by bogdanbrudiuMorteza Manavi)

參考文件

  1. EntityFramework FluentAPI mapping issue (CC BY-SA 3.0/4.0)

#mapping #fluent-interface #entity-framework-4






相關問題

Hibernate 單向父/子關係 - delete() 對子表執行更新而不是刪除 (Hibernate Unidirectional Parent/Child relationship - delete() performs update on child table instead of delete)

TIGER shapefile - 使用和解釋 (TIGER shapefiles - using and interpreting)

查找位置的城市和郵政編碼 (Finding City and Zip Code for a Location)

如何使 Vim 插件 NERDTree 的 <CR> 表現得更像 `go`? (How to make Vim plugin NERDTree's <CR> behave more like `go`?)

在 NHibernate 上的多對多關係上添加自定義列 (Add custom columns on many to many relationship on NHibernate)

在大型 .NET 項目中管理 DTO 和映射 (Managing DTOs and mapping in large .NET project)

使用 Fluent NHibernate 進行繼承映射 (Inheritance Mapping with Fluent NHibernate)

Biztalk映射問題,請想法 (Biztalk Mapping Problem, Ideas please)

BizTalk 數據庫查找 functoid 固定條件 (BizTalk Database Lookup functoid fixed condition)

EntityFramework FluentAPI 映射問題 (EntityFramework FluentAPI mapping issue)

將外鍵添加到現有數據庫 (Adding foreign keys to an already existing database)

如何重命名對像數組中對象的所有鍵? (How does one rename all of an object's keys within an array of objects?)







留言討論